Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Class Loading Question

Thread view: 
Hal Vaughan - 26 Jul 2006 23:16 GMT
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
Vincent van Beveren - 27 Jul 2006 08:06 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.