4

I have a Glassfish 3.1.1 web application for Java EE 6 running. one of my classes has a native library dependency. I'm using JNI to use this native method. Part of that process involves creating a shared library .so file. In one of my Java classes there will be a System.Loadlibrary("library"); call that references that library.so file.

My questions is, where should I place this shared library as well as the native code that it references in order to access and use this JNI functionality on Glassfish.

I'll need to make calls to several C++ programs that can be moved anywhere on the machine that that Glassfish is hosted on. Should I put them in the domain1/ext folder? add them as a native library to a jar in the build path. How is this done?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Randnum
  • 1,360
  • 4
  • 32
  • 48
  • I recommend reading the accepted answer to this question: http://stackoverflow.com/questions/1962718/maven-and-the-jogl-library – Alexandre Jan 20 '12 at 20:38

2 Answers2

8

Use the following Glassfish command to set the path where your native libraries are located:

asadmin set server.java-config.native-library-path-prefix=$NATIVE_LIBRARY_PATH

It is also possible to set the native library location via the Admin Console. If you want flexibility in moving/renaming the libraries, you can use symbolic links.

What is the advantage of this to adding a native library through Eclipse build path?

The JVM needs to know the native library path. If you launch the JVM from Eclipse (e.g. unit testing with the native library), then you will need to configure the path in Eclipse. However, if you launch the JVM from Glassfish (by using the Eclipse Glassfish Adapter) then Glassfish itself needs to be configured.

Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
  • +1. Definitely better than just sticking your own libraries into system directories. – andri Jan 20 '12 at 20:33
  • What is the advantage of this to adding a native library through Eclipse build path? if I go to the build path select the jar. Expand a jar I can edit somthing called "Native Libraries" what does this do and how is it used differently than setting the asadmin server.java-config... setting. – Randnum Jan 20 '12 at 21:46
  • If you're still checking this does this also apply to making the project readyto be deployedon another server? I'm assuming tht if I consistently set the files in the same directories on the server I can use the same asadmin call and set the $NATIVE_LIBRARY_PATH on each machine? – Randnum Jan 24 '12 at 00:19
  • The command should work on multiple Glassfish servers so long as the native libraries are in the same place on each server. You could make the asadmin commands part of your production server build. – Garrett Hall Jan 24 '12 at 16:21
  • @GarrettHall What about libs that depend on other libs, and thus need a setting of LD_LIBRARY_PATH? GF3.1.1 does not seem to pass LD_LIBRARY_PATH settings down to the child jvms that it creates. – bmargulies Apr 18 '12 at 14:41
  • I think you need to export the variable in the parent shell of the Glassfish process. Glassfish runs on top of a JVM, so I'm not sure what you mean by child JVMs. – Garrett Hall Apr 19 '12 at 17:26
-2

When I Use a JNI Wrapped DLL in Windows Based server, Have Place the DLL in

    C:/Windows/System32

You can try placing .so on linux machine at

   /usr/lib64/

just a try.

Suave Nti
  • 3,721
  • 11
  • 54
  • 78