Dynamic Jar Loading...

Discussions

Performance and scalability: Dynamic Jar Loading...

  1. Dynamic Jar Loading... (1 messages)

    Let's say I had a jar file called "myjar.jar" and within this jar file is a class called "mypackage.MyClass". Why wouldn't the following work...?

    java.net.URLClassLoader.newInstance(new java.net.URL[]{new java.io.File("myjar.jar").toURL()});
    new mypackage.MyClass();

    I can't seem to get any method of dynamically loading jar files to work. No matter what I do, I get a java.lang.ClassNotFoundException thrown.

    It is my understanding that java.net.URLClassLoader will load classes from the directories or jar files that were specified as resources. When the JVM needs to load a class, it will first attempt to load a class from the system classloader, and then from it's child classloaders. By default the java.net.URLClassLoader sets the system classloader as it's parent.

    Is there anyway to set the system classloader to something different? In any case, I would really like to make a classloader that loads recourses from a directory and it's *sub-directories*. Sort of like the JVM's ext directory except with subdirectory inheritance support.

    More info can be found here:
    http://java.sun.com/docs/books/tutorial/ext/basics/load.html
    http://java.sun.com/j2se/1.4/docs/api/java/net/URLClassLoader.html

    Thanks!
  2. Dynamic Jar Loading...[ Go to top ]

    You have it the wrong way round. The child class loader refers back to it's parent class loader. More specifically:

    <The ClassLoader class uses a delegation model to search for classes and resources. Each instance of ClassLoader has an associated parent class loader. When called upon to find a class or resource, a ClassLoader instance will delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself.> (quote from J2SE API v1.3.1)

    Once you've created your URLClassLoader referring to the new jar(s), use the findClass(String classname) method to retrieve a Class object, then use the reflection API to create instances of the new class. Classes loaded by the parent ClassLoader(s) will have to use your URLClassLoader instance and the reflection API to create instances of the classes in the jar(s), but the classes in the jar will have full access to all the classes in the new jar(s) and in the parent classloader(s) without using reflection.

    If you want to do the sub-directory searching class loader, just override the URLClassLoader class and provide a constructor looking like:

    public DeepURLCLassLoader(File directory)
    {
      //some form of recursive iteration through the directory passed in
      //to find all jars beneath it (pretty standard java io) and create URL[]
      URL[] jars = findAllJarsIn(directory);
      super(jars);
    }

    Hope this helps, but I'm not quite sure that this is the correct discussion for this kind of question (mind you I can't see one that is better suited).

    Bob B.