Preloading java classes/libraries at jar startup?

Solution 1:

You could run a load before the server becomes live. You haven't specified how you're loading the server, the classes, and what the environment is, but you can take advantage of the fact that a class static initializer will run when the class is loaded. So, if you're running from a "main" method, your class could look something like this

public class Foo {

   static {
     //this will be run when the class is loaded
     try { Class.forName("fully.qualified.class.name.that.i.want.to.Load"); }
     catch ...
   }

   public static void main (string args[])
   {
    //run my server...
   }
}

Solution 2:

One thing you might want to try is writing a simple client inside of the Java server itself. This client does nothing but call some method in the server when it starts up, forcing the classes to be loaded. After this little client gets a result (or callback), then it puts the server into an "accessible by the outside world" state.

Solution 3:

I suggest faking a connection just before opening up the server. That will ensure that (most) relevant lazy initialisation will have been performed.