I need to add plugin functionality to an existing application for certain parts of the application. I want to be able to add a jar at runtime and the application should be able to load a class from the jar without restarting the app. So far so good. I found some samples online using URLClassLoader and it works fine.
I also wanted the ability to reload the same class when an updated version of the jar is available. I again found some samples and the key to achieving this as I understand is that I need to use a new classloader instance for each new load.
I wrote some sample code but hit a NullPointerException. First let me show you guys the code:
package test.misc;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import plugin.misc.IPlugin;
public class TestJarLoading {
public static void main(String[] args) {
IPlugin plugin = null;
while(true) {
try {
File file = new File("C:\\plugins\\test.jar");
String classToLoad = "jartest.TestPlugin";
URL jarUrl = new URL("jar", "","file:" + file.getAbsolutePath()+"!/");
URLClassLoader cl = new URLClassLoader(new URL[] {jarUrl}, TestJarLoading.class.getClassLoader());
Class loadedClass = cl.loadClass(classToLoad);
plugin = (IPlugin) loadedClass.newInstance();
plugin.doProc();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
IPlugin is a simple interface with just one method doProc:
public interface IPlugin {
void doProc();
}
and jartest.TestPlugin is an implementation of this interface where doProc just prints out some statements.
Now, I package the jartest.TestPlugin class into a jar called test.jar and place it under C:\plugins and run this code. The first iteration runs smoothly and the class loads without issues.
When the program is executing the sleep statement, I replace C:\plugins\test.jar with a new jar containing an updated version of the same class and wait for the next iteration of while. Now here's what I don't understand. Sometimes the updated class gets reloaded without issues i.e. the next iteration runs fine. But sometimes, I see an exception thrown:
java.lang.NullPointerException
at java.io.FilterInputStream.close(FilterInputStream.java:155)
at sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream.close(JarURLConnection.java:90)
at sun.misc.Resource.getBytes(Resource.java:137)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:256)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at test.misc.TestJarLoading.main(TestJarLoading.java:22)
I have searched on the net and scratched my head but can't really arrive at any conclusion as to why this exception is thrown and that too - only sometimes, not always.
I need your experience and expertise to understand this. What's wrong with this code? Please help!!
Let me know if you need any more info. Thanks for looking!
This is an update tested on java 7 with success. Now the
URLClassLoader
works fine for meMyReloader
Close and Restart the ClassReloader
then update your jar and call
then you can restart the app with the new version.
Do not include your jar into your base class loader
Do not include your jar into your base class loader "
MyReloaderMain.class.getClassLoader()
" of the "MyReloaderMain
", in other words develop 2 project with 2 jars one for "MyReloaderMain
" and the other one for your real application without dependency between the two, or you will not able to understand who i loading what.The error is still present in
jdk1.8.0_2
5 onWindows
. Although @Nicolas' answer helps, I hit aClassNotFound
forsun.net.www.protocol.jar.JarFileFactory
when running it on WildFly, and several vm crashes while debugging some box tests...Therefore I ended up extracting the part of the code which deals with loading and unloading, to an external jar. From the main code I just call this with
java -jar....
all looks fine for now.NOTE: Windows does release the locks on the loaded jar files when the jvm exits, that is why this works.
Starting from Java 7, you indeed have a
close()
method inURLClassLoader
but it is not enough to release completely the jar files if you call directly or indirectly methods of typeClassLoader#getResource(String)
,ClassLoader#getResourceAsStream(String)
orClassLoader#getResources(String)
. Indeed by default, theJarFile
instances are automatically stored into the cache ofJarFileFactory
in case we call directly or indirectly one of the previous methods and those instances are not released even if we calljava.net.URLClassLoader#close()
.So a hack is still needed in this particular case even with Java 1.8.0_74, here is my hack https://github.com/essobedo/application-manager/blob/master/src/main/java/com/github/essobedo/appma/core/util/Classpath.java#L83 that I use here https://github.com/essobedo/application-manager/blob/master/src/main/java/com/github/essobedo/appma/core/DefaultApplicationManager.java#L388. Even with this hack, I still had to call the GC explicitly to fully release the jar files as you can see here https://github.com/essobedo/application-manager/blob/master/src/main/java/com/github/essobedo/appma/core/DefaultApplicationManager.java#L419
This behaviour is related to a bug in the jvm
2 workarounds are documented here
For everyone's benefit, let me summarize the real problem and the solution that worked for me.
As Ryan pointed out, there is a bug in JVM, which affects Windows Platform.
URLClassLoader
does not close the open jar files after it opens them for loading classes, effectively locking the jar files. The jar files can't be deleted or replaced.The solution is simple: close the open jar files after they've been read. However, to get a handle to the open jar files, we need to use reflection since the properties we need to traverse down are not public. So we traverse down this path
The code to close the open jar files can be added to a close() method in a class extending URLClassLoader:
(This code was taken from the second link that Ryan posted. This code is also posted on the bug report page.)
However, there's a catch: For this code to work and be able to get a handle to the open jar files to close them, the loader used to load the classes from the file by URLClassLoader implementation has to be a
JarLoader
. Looking at the source code ofURLClassPath
(methodgetLoader(URL url)
), I noticed that it uses a JARLoader only if the file string used to create the URL does not end in "/". So, the URL must be defined like this:The overall class loading code should look something like this:
Update: JRE 7 has introduced a
close()
method in the classURLClassLoader
which may have solved this issue. I haven't verified it.