By Date: <-- -->
By Thread: <-- -->

JNI native libraries and Tomcat reloading web-apps



Hello,

I have a question using native JNI shared objects within a servlet running under Tomcat 5.5.  Our servlet code depends on some shared .dll files (for windows, and .so files for Linux).  so we have classes that have code such as:

public class Abc {
    public native static boolean nativeF0(int a,String b);
    /* ... other native method declarations ... */
    static {
        System.loadLibrary("objectfile");
    }
}

Obviously the idea is that the shared object objectfile.dll is loaded whenever the native methods are accessed.  However, Tomcat seems to create a new Java class-loader whenever a web-app is reloaded via using the Tomcat "manager" web-app.  Because of the new class-loader, the "static { System.loadLibrary(...); }" method is re-executed and the 2nd call to loadLIbrary yields an JRE error "java.lang.UnsatisfiedLinkError: Native Library objectfile.dll already loaded in another classloader" followed by a JRE crash.

The workaround is to completely stop and restart the entire Tomcat server every time I modify my web-app.  But this is cumbersome and makes our web-app harder to deploy and patch.  Catching and ignoring the UnsatisfiedLinkError solves nothing because the classes loaded in the 2nd class-loader can't find the native methods.

Can someone please give me some advice as to how to do one of the following:
    a.  Unload my original objectfile.dll when my web-app is stopped so when it is restarted it loads normally in the new class-loader
    b.  Connect the native libraries from the first class-loader to the second class-loader after the web-app is reloaded
    c.  Other possibilities to work around this?

Thank you,
Eric Johanson