Tuesday 16 July 2013

Adding a library to java library path in Ubuntu

To enable a Java Native Interface (JNI) access to a non-java library, such as BerkeleyDB, then the library needs to be on the library path.

The simple answer given by Oracle and others is to run the command:

java -Djava.library.path=/usr/local/BerkelyDB.5.1/lib MyClass

That is a pain and difficult to ensure happens for all invocations of java.

It would be nice to use Ubuntu's ldconfig, but this is ignored by java! Java will however load from an environment variable LD_LIBRARY_PATH.

A sensible thing to do would be to place this variable in a system wide config, as it is a pain to add to every invocation of java, however due to supposed security issues this environment variable cannot be set in the normal places (/etc/environment, /etc/profile.d/) as it is deliberately nulled if you do set it!.

Sadly the only way is to add the following to the .bashrc of every user which will use your library (root, jenkins, jetty and yourself)

export LD_LIBRARY_PATH=/usr/local/BerkeleyDB.5.1/lib