3

There is a library (jar file) needed for two of our web applications on Tomcat 6.0.35. And one of the class in the jar file requires a native library to run.

As expected, i am getting exception

java.lang.UnsatisfiedLinkError: Native Library my-native-library.so 
already loaded in another classloader

if two applications are deplayed at the same time.

Within the jar file, native library is loaded using a static block

static
{
    try
    {           
        System.loadLibrary("my-native-library.so");

    } catch (UnsatisfiedLinkError e)
    {
        e.printStackTrace();
    }
}

and the class that using the native library is configurable using xml.

The jar library is an search engine built by us, so we can rebuild it if necessary.

So my question is how can we tell Tomcat that my-native-library.so has been loaded by another web application, and the static block can be ignored for this application.

EDIT: Both jar library and the web applications are using maven.

Thanks.

EDIT: By my understanding:

The search engine library needs the my-native-library.jar file to build into a jar library. Then the search engine jar library is hosted on our repository and build into our web application using maven. When server is started, the LD_LIBRARY_PATH and JAVA_OPTS with shared lib path are exposed, server is able to load my-native-library.so once only. However, both web applications contain the search engine jar and my-native-library.jar will be in the WEB_INF/lib, and exceptions.

To remove my-native-library.jar from WEB_INF/lib, the search engine library needs to be rebuild without the necessary dependency (my-native-library.jar).

user200340
  • 3,301
  • 13
  • 52
  • 74
  • There are a few dups of this question, with good information but no accepted answers. I would recommend starting [here](http://stackoverflow.com/questions/3724421/native-library-already-loaded-in-another-classloader). – Perception Mar 26 '13 at 10:54

1 Answers1

1

In that case I would load the library in a shared classloader in Tomcat, so that it is only loaded once in the system. Look at Tomcat class loaders documentation for more details:

http://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html

Kurt
  • 36
  • 3
  • 1
    Thanks Kurt, I have tried the following (1) copy my-native-library.so to ${catalina.base}/shared/lib (2) copy my-native-library.jar to ${catalina.base}/lib (3) modify catalina.properties shared classloader to shared.loader=${catalina.base}/shared/lib (4) add -Djava.library.path="/home/me/apache-tomcat-6.0.35/shared/lib" argument to eclipse. Howeve, I am still getting the same exception. Did I miss anything? – user200340 Mar 26 '13 at 11:57
  • The steps you post seem correct. However, the fact that you are getting the "library loaded in another classloader" proves that it is being loaded more than once. Check that libray is not under WEB-INF/lib o WEB-INF/classes directory, and that the web application do not use a custom class loader that is loading the library. Class loader issues can get very complicated and hard to troubleshoot. – Kurt Mar 26 '13 at 13:38