Class Loading Question

H

Hal Vaughan

I have a program that needs to be able to run as an application and an
applet. One big concern with it running as an applet is loading time.
There are a number of classes that the program will need as an application
that deal with things handled by the CGI backend on the applet. I'm most
concerned about who Java and browser handle loading classes in an applet.
Do they load all the classes when the applet is invoked, or when classes
are first used?

If I have:

if (isApplet) {
//Do applet stuff here
} else {

MyClass mc = new MyClass;
mc.verify(userName, passWord);
mc.output("results");
}

(Of course, isApplet would only be true if the program were running as an
applet.)

When the program is run as an applet, I know MyClass has to be available,
but will it also be loaded? If so, is there a way to get around it and not
load classes unless the program actually calls them?

Thanks for any background on this.

Hal
 
V

Vincent van Beveren

Hal Vaughan schreef:
When the program is run as an applet, I know MyClass has to be available,
but will it also be loaded? If so, is there a way to get around it and not
load classes unless the program actually calls them?

Hi Hal,

The jar file(s) you specify at in the archive attribute are always
loaded prior to the application start up. The only thing you might be
able to do is use an java.net.URLClassLoader to load specific modules.
For example:


private Runnable clientModule;

void enterClient() {

if (clientModule == null) {
// maybe display message 'please wait loading module'
URLClassLoader urlc = URLClassLoader.getInstance(
"clientModule.jar", getClass().getClassLoader());
try {
clientModule = (Runnable)
urlc.loadClass("my.package.ClientModule")
.newInstance();
} catch (Exception e) {
// better exception handling
}
}

clientModule.run();

}

Something like that. You might have some security issues though.

Vincent
 
V

Vincent van Beveren

URLClassLoader urlc = URLClassLoader.getInstance(
"clientModule.jar", getClass().getClassLoader());

I made a little error here. The "clientModule.jar" should be an URL. The
best thing is to use the following construct:

URL url = new URL(applet.getCodeBase(), "clientModule.jar");

This makes sure the applet knows you are in the security domain. Besides
that when running in client mode you can load all modules at start up
and you can change the URL loading class code into a shorter version:

if (isApplet()) {
// do url loading stuff I desribed
} else {
try {
clientModule = (Runnable)
class.forName("my.package.Module").newInstance();
} catch (Exceptions e) {
// better exception handling here
}
}
clientModule.run();

Besides that you can create a base interface in the core package called
'Module' or something that allows for more flexibility. For example:

interface Module {

String getDescription();

void execute(Object... params);
}

Because you can't use the actual classes in your core jar file, you'll
need to abstract it like that. If you do use the actual classes, you'll
need to include them in the core jar, which is exactly what we don't want...

Vincent
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top