Loading a class using the applet class loader

Discussion in 'Java' started by =?iso-8859-1?q?Andr=E9_Wagner?=, May 30, 2007.

  1. 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é
     
    =?iso-8859-1?q?Andr=E9_Wagner?=, May 30, 2007
    #1
    1. Advertising

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

    Eric Sosman Guest

    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?

    --
     
    Eric Sosman, May 30, 2007
    #2
    1. Advertising

  3. > 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é
     
    =?iso-8859-1?q?Andr=E9_Wagner?=, May 30, 2007
    #3
  4. =?iso-8859-1?q?Andr=E9_Wagner?=

    Mark Space Guest

    André Wagner wrote:
    > 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.
     
    Mark Space, May 30, 2007
    #4
  5. > 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é
     
    =?iso-8859-1?q?Andr=E9_Wagner?=, May 31, 2007
    #5
  6. 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
     
    Andrew Thompson, May 31, 2007
    #6
  7. =?iso-8859-1?q?Andr=E9_Wagner?=

    Mark Space Guest

    André Wagner wrote:

    > 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, May 31, 2007
    #7
  8. =?iso-8859-1?q?Andr=E9_Wagner?=

    Mark Space Guest

    Mark Space wrote:

    > 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.
     
    Mark Space, May 31, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Archana
    Replies:
    1
    Views:
    1,406
    Andrew Thompson
    Oct 25, 2004
  2. Replies:
    1
    Views:
    586
    Andrea Desole
    Dec 14, 2005
  3. Wayne Gibson

    Applet Loader

    Wayne Gibson, Jan 10, 2006, in forum: Java
    Replies:
    3
    Views:
    2,102
    IchBin
    Jan 10, 2006
  4. Karsten Wutzke
    Replies:
    0
    Views:
    404
    Karsten Wutzke
    Jun 29, 2007
  5. Replies:
    1
    Views:
    219
    Laurent Bugnion
    Jun 9, 2006
Loading...

Share This Page