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
Vincent van Beveren - 27 Jul 2006 08:15 GMT
> 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