Search code examples
javatomcatweb-applicationsjava-native-interfaceshared-libraries

Shared JNI library (.so) in Tomcat - UnsatisfiedLinkError


I have a JNI library (.so) shared between two web applications deployed in Tomcat7. I am loading the library using the System.loadLibrary only once in the first web application that is being deployed and then in the second I'm checking if it already was loaded to not load anymore (I tried loading it in both and I got UnsatisfiedLinkError - library was loaded by another classloader). I can make any call to the native library in the first application, but in the second one I get UnsatisfiedLinkError with the method name that I am trying to call.

I am running out of ideas of what I can do. Any ideas? I tried most of the solutions on SO. Thank you.

EDIT Yes, I tried adding the library in the tomcat lib folder and loading it from there. Initially it was in the bin folder and the same issue occurs.


Solution

  • Yes, this will happen when you try to load the library that has already loaded my another web application. Tomcat, uses separate class loaders for each of the web application, and it wont allow you load a same native library more than once to JVM via another class loader

    Move any share jar files if any that consumes JNI from you sharedlib.so. Add the system path to sharedlib ,

    export LD_LIBRARY_PATH=/path/to/whereyourlinklibrary
    

    Write a simple class like this which enables you to load your shared library when tomcat starts. Just compile this class and drop it in tomcat lib folder

    package msm;
    public class DLLBootstrapper {
    
         static {
          System.loadLibrary("sharedlib");
         }
    
         public static void main(String args[]) {
          System.out.println("Loaded");
         }
    
        }
    

    you can now load this class from any of your web application ( probably in startup listener)

    Class.forName("msm.DLLBootstrapper");
    

    Good to go!