Hello,
I'm loading classes during the execution of a applet, like this:
ClassLoader cl = getClass().getClassLoader();
Class c = cl.loadClass(className);
MyObject o = (MyObject)c.newInstance();
It works fine, but if the class is big, the applet execution hangs
until the whole class is downloaded from the server and loaded. Is
there any way I can download the class (showing the user a progressbar
for that) and load the bytecode? I tried using the defineClass method,
but I can't overwrite in the applet class loader it because of the
applet security restrictions.
Thank you in advance,
André
Eric Sosman - 30 May 2007 19:23 GMT
André Wagner wrote On 05/30/07 14:04,:
> Hello,
>
[quoted text clipped - 10 lines]
> but I can't overwrite in the applet class loader it because of the
> applet security restrictions.
Perhaps an expert will think of something, but I
doubt what you ask for is possible. However, could
you run a separate Thread to load the class while the
rest of your applet keeps on executing?

Signature
Eric.Sosman@sun.com
André Wagner - 30 May 2007 19:41 GMT
> Perhaps an expert will think of something, but I
> doubt what you ask for is possible.
I see...
> However, could
> you run a separate Thread to load the class while the
> rest of your applet keeps on executing?
Well, while this is not what I had in mind, it certainly is better
than hanging the applet. Thank you for the hint.
André
Mark Space - 30 May 2007 21:04 GMT
> Hello,
>
[quoted text clipped - 10 lines]
> but I can't overwrite in the applet class loader it because of the
> applet security restrictions.
I don't see why not, but I'm not an expert, so take this with a grain of
salt.
Subclass an appropriate classloader so you have your own classloader to
play with. Probably, you will need to subclass URLClassLoader.
Override the findClass() method to check the size of any class it finds,
and throw up a progress bar if the size is over X, where X is some
number of bytes that you think will take "too long" to download. Then
use your new classloader to load the rest of the app (you'll need a
small stub at the beginning of the app to start things off).
Alternate, make your own classloader and override it's findClass()
method. Make a second class that just does a regular old HTTP or FTP
download of a lot of different stuff: several classes and resources. In
other words, this second class downloads everything in one go. Your own
classloader in this case will know about this second class and look
there for any files first, before handing them off to the default
classloader. This way, you can download everything in one shot, and
have it available so the user doesn't have to wait.
Just a couple of ideas. I've never tried this, so good luck.
André Wagner - 31 May 2007 12:24 GMT
> Just a couple of ideas. I've never tried this, so good luck.
Thank you for the hints, but unfortunately, this is impossible. An
applet has a series of security restrictions, and one of these
restrictions is that a applet can never subclass or create a class
loader.
André
Mark Space - 31 May 2007 20:48 GMT
> Thank you for the hints, but unfortunately, this is impossible. An
> applet has a series of security restrictions, and one of these
> restrictions is that a applet can never subclass or create a class
> loader.
Huh, this is new to me. Where did you hear this at? My brief search
didn't find anything that hinted at this...
Mark Space - 31 May 2007 20:51 GMT
> Huh, this is new to me. Where did you hear this at? My brief search
> didn't find anything that hinted at this...
nm, found one. Applet classloaders can easily be sub-classed, but the
browser running it won't let you (normally). Sorry about that.
Andrew Thompson - 31 May 2007 13:42 GMT
..
>I'm loading classes during the execution of a applet, ...
Why?
...
>It works fine, but if the class is big, the applet execution hangs
>until the whole class is downloaded from the server and loaded. Is
>there any way I can download the class (showing the user a progressbar
>for that) and load the bytecode?
It might be possible to offer this information if the applet
is launched using web start, the classes added to the
classpath and specified as a 'lazy' download.
Here is my latest best effort to achieve the lazy
downloads with a progress dialog.
<http://www.physci.org/jws/cache/#latesteg>
This example deals with images as the resources,
but the API is more intended for classes.
I experienced some problems with the progress
dialog offered, that I have not had time to resolve.
Maybe you can track down solutions.
HTH

Signature
Andrew Thompson
http://www.athompson.info/andrew/