Question

If I'm running a signed Java applet, can I load additional classes from remote sources (in the same domain, maybe even the same host) and run them?

I'd like to do this without changing pages or even stopping the current applet. Of course, the total size of all classes is too large to load them all at once.

Is there a way to do this? And is there a way to do this with signed applets and preserve their "confidence" status?

Was it helpful?

Solution

I think classes are lazy loaded in applets. being loaded on demand.

Anyway, if the classes are outside of a jar you can simply use the applet classloader and load them by name. Ex:

ClassLoader loader = this.getClass().getClassLoader();
Class clazz = loader.loadClass("acme.AppletAddon");

If you want to load classes from a jar I think you will need to create a new instance of URLClassLoader with the url(s) of the jar(s).

URL[] urls = new URL[]{new URL("http://localhost:8080/addon.jar")};
URLClassLoader loader = URLClassLoader.newInstance(urls,this.getClass().getClassLoader());
Class clazz = loader.loadClass("acme.AppletAddon");

By default, applets are forbidden to create new classloaders. But if you sign your applet and include permission to create new classloaders you can do it.

OTHER TIPS

Yes, you can open URL connections to the host you ran your applet from. You can either create a classloader with HTTP urls, or download the classes (as jars) to the user's machine and create a classloader with those jars in the classpath. The applet won't stop and you don't need to load another page.

Regarding the second part of your question about confidence, once the user has granted access to your applet it can download anything, yes anything, it wants to the local machine. You can probably inform the user as to what it's doing, if your UI design permits this.

Hope this helps.

Sounds like it should be possible (but I've never done it). Have you already had a look at Remote Method Invocation (RMI)?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top