user200340
user200340

Reputation: 3371

How to handle shared native library in multiple web applications on Tomcat

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).

Upvotes: 3

Views: 7056

Answers (1)

Kurt
Kurt

Reputation: 36

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

Upvotes: 1

Related Questions