degratnik degratnik - 1 year ago 75
Java Question

System.loadLibrary does not work. UnsatisfiedLinkError for the second lib in chain

I have java program Client.class that uses cpp shared library via JNI. is built as shared and uses cpp shared library and are placed in folder


Client.class is placed in

Client can load library with

  1. System.load and environment variable LD_LIBRARY_PATH

  2. System.loadLibrary and -Djava.library.path

The first way works fine.

export LD_LIBRARY_PATH = /home/client/lib64

java -classpath ./bin Client

The secon way fails.

java -classpath ./bin -Djava.library.path=./../lib64 Client

java.lang.UnsatisfiedLinkError: /home/client/lib64/ cannot open shared object file: No such file or directory

When I put into /usr/lib64 the second way works fine.

Why is looking for in /usr/lib64 if I use System.loadLibrary?
How can I fix it without coping into /usr/lib64?

My loading code:

//Try load from -Djava.library.path
boolean found = false;
String lib = "client";
try {
found = true;
} catch (UnsatisfiedLinkError e) {
//Try load from LD_LIBRARY_PATH
if (!found) {
lib = "";
String ld_lib_path = System.getenv("LD_LIBRARY_PATH");
String[] paths = ld_lib_path.split(":");
for(int i=0; i<paths.length; i++) {
String p = paths[i];
File x = new File(p, lib);
if (x.exists()) {
found = true;

Additional information.

If I test with ldd then I see: => not found
If I set export LD_LIBRARY_PATH = /home/client/lib64 then I see: => /home/client/lib64/

Answer Source

The reason for this is that is loaded from your JVM, which looks in java.library.path. However, when tries to load, it knows nothing about Java and just uses the regular Linux way of loading shared libraries (the dynamic linker, which looks in LD_LIBRARY_PATH and some common directories like /usr/lib64.

I would probably go with using LD_LIBRARY_PATH set from a start script of your Java application. If you don't want to use a start script, you could in theory set LD_LIBRARY_PATH from within the process itself. However, Java does not allow to do this (there is only System.getenv(), not System.setenv()), so you would need to write a small C library that is called from Java and calls putenv() setting LD_LIBRARY_PATH.

If you build itself, you can use the -rpath linker flag to specify a path where the dynamic linker should look for further required libraries. Be careful if you specify a relative path here, it will interpreted as relative to the current working directory of the running application, not relative to the location of To achieve this, you need to use $ORIGIN as argument for -rpath and be careful that your shell does not expand this.

So, if you want to have and in the same directory, you need to use

-rpath '$ORIGIN'

as argument to the linker when building If you do not call the linker directly but let your compiler call it, you need to add the following to your compiler's command line:


More information about this can be found in the man page for