Loading a class using the applet class loader

  • Thread starter =?iso-8859-1?q?Andr=E9_Wagner?=
  • Start date
?

=?iso-8859-1?q?Andr=E9_Wagner?=

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é
 
E

Eric Sosman

André Wagner wrote On 05/30/07 14:04,:
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.

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?
 
?

=?iso-8859-1?q?Andr=E9_Wagner?=

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é
 
M

Mark Space

André Wagner said:
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.

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.
 
?

=?iso-8859-1?q?Andr=E9_Wagner?=

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é
 
A

Andrew Thompson

André Wagner wrote:
..
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

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200705/1
 
M

Mark Space

André Wagner said:
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...
 
M

Mark Space

Mark said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top