Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java web application cannot use native library (.so)

Technical summary: I'm developing a Java web service deployed on GlassFish v3, running on CentOS 5.

My web service uses functionality provided by a native library (.so) . The native library works fine, however I am not having much luck in configuring the environment correctly to load the native library yet not be affected by web app re-deployment, without restart the app server.

What I've done so far is:

Initially I loaded the library (static {System.load(path/to/libabc.so)};) in the web service code, all paths set correctly, and it works fine, until I re-deploy the application and it complains that the library is loaded by another ClassLoader. I found out that native libs are only loaded once.

To try and solve this I then removed the library-loading code from the web application, created a Singleton class, wrapped it into a Lifecyle module, deployed it to GlassFish shared lib folder and then configured GlassFish to run the wrapper when it starts up. The idea being that now all web applications would be able to reference it since it is not tied to one particular web app and loaded by a ClassLoader higher in the hierarchy.

When GlassFish starts up the native library is loaded successfully ( linux> lsof | grep libabc.so ). However, the web service code fails with a UnsatisfiedLinkError when executing the native method in my web service Java code. It seems to me that code in the web application does not have access to the library loaded at start-up.

Can anyone tell me what I'm doing wrong?

Thanks in advance.

like image 884
water-sparks Avatar asked Feb 23 '10 14:02

water-sparks


2 Answers

I can't say much about "Lifecyle module" (I don't know if they are supposed to be "visible" to applications deployed to GlassFish) but...

I would indeed put the JNI library and the class that calls System.loadLibrary(String), for example a singleton, outside the webapp and deploy this code in domain/lib or domain/lib/applibs (see File Layout in V3 and this thread for more background on them).

This should make the code visible to your application and your application resistant to redeployment.

like image 155
Pascal Thivent Avatar answered Nov 11 '22 22:11

Pascal Thivent


solved!

Finally,i put the pieces together.

missing piece Added the JNI library (e.g. jni_wrapper_for_libabc.jar) to GF shared folder domains/domain1/lib and it worked. The native lib is loaded by a Singleton class in a Lifecycle Module which is called when GF starts up.

Thank you very much Pascal, great help mate!!

Cheers

like image 39
water-sparks Avatar answered Nov 11 '22 23:11

water-sparks