RSS Feed for This PostCurrent Article

Java – Dynamic loading of class and jar file

Download Source Code

Additional Download


I was asked this question recently to load class or jar file dynamically in Java application. There are few things to take note here

Dynamic Class Loading

To load a class dynamically is straightforward. Just make sure the classpath is defined when your program starts up. E.g., if you define /myapp/classes folder as your classpath during start up, and you have created a new class called com.myapp.NewClass aftert this. All you need to do is to copy NewClass into /myapp/classes/com/myapp


Dynamic Jar Loading

To load a jar dynamically is a bit tricky. There are 2 parts here

  1. You can load the jar using URLClassLoader. But the existing classpath that you defined during application startup is not changed
  2. To change the existing classpath, you have to do a bit of hacking to use reflection to modify the existing classpath.

Below is the code snippet I used to load a jar file using URLClassLoader

URLClassLoader clazzLoader ;
Class clazz;
try {
ClassLoaderUtil.addFile(filePath);
filePath = “jar:file://” + filePath + “!/”;
URL url = new File(filePath).toURL();
clazzLoader = new URLClassLoader(new URL[]{url});
clazz = clazzLoader.loadClass(name);
return clazz.newInstance();

…….

Take note of the jar:file:// pattern. ClassLoaderUtil will add the new jar file to the existing classpath

public static void addURL(URL u) throws IOException {

URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
URL urls[] = sysLoader.getURLs();
for (int i = 0; i < urls.length; i++) {
if (StringUtils.equalsIgnoreCase(urls[i].toString(), u.toString())) {
if (log.isDebugEnabled()) {
log.debug(“URL ” + u + ” is already in the CLASSPATH”);
}
return;
}
}
Class sysclass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod(“addURL”, parameters);
method.setAccessible(true);
method.invoke(sysLoader, new Object[]{u});
} catch (Throwable t) {
t.printStackTrace();
throw new IOException(“Error, could not add URL to system classloader”);
}
}

This also bring me another question. How to dynamically unload and reload a class or Jar file ? So far I still don’t know how to do this yet.


Trackback URL


RSS Feed for This Post9 Comment(s)

  1. Ink | Oct 30, 2007 | Reply

    use OSGI to these type of operations

  2. Josh | Feb 5, 2008 | Reply

    Given the standard class name like(“package1.className”), you could load a class dynamically using java.lang.reflect.Class’s static forName method:
    Class c = Class.forName(“yourClass”);

    .. but I want to know how to get an appropriate class name from a file path. That first example omits explaining where the “name” variable came from. It is easy when there are no packages to strip out paths and the file extension but how can you get the appropriate class name with its preceding packages?

  3. admin | Feb 5, 2008 | Reply

    Attached the additional code for download..

  4. Adam | Feb 22, 2008 | Reply

    > How to dynamically unload and
    > reload a class or Jar file ?

    http://site.red-illusions.dk/2006/11/04/simple-dynamic-loadingunloading-of-code/

  5. xeus | Mar 28, 2008 | Reply

    try using JCL… http://jcloader.sourceforge.net

  6. Heff | Apr 7, 2008 | Reply

    You don’t need to unload java classes, the classes will be garbage collected once the classloader is no longer referenced by any objects on the heap. Remember that Classes are also Objects.

  7. Kris | May 7, 2008 | Reply

    From where can I get the class ClassLoaderUtil ?

    and why is it needed ?

  8. sandipan | Jun 12, 2008 | Reply

    after loading a class using the above method i am not able to delete the jar file. The url class loader is not releasing the handle of the jar.

    How can i delete the jar after class loading?

  9. shahryar ghazi | Jun 14, 2009 | Reply

    thanks for sharing .. very helpful

3 Trackback(s)

  1. From Develop a Java Plugin Framework | twit88.com | Oct 7, 2007
  2. From the rasx() context » Blog Archive » My Humble Java | Nov 7, 2008
  3. From Dynamic Class Loading from external Jar - Java Forums | Feb 8, 2010

Sorry, comments for this entry are closed at this time.