I have java program Client.class that uses cpp shared library libclient.so via JNI. libclient.so is built as shared and uses cpp shared library libhttp.so.
libclient.so and libhttp.so are placed in folder /home/client/lib64
Client.class is placed in /home/client/bin
Client can load library with
- System.load and environment variable LD_LIBRARY_PATH
- 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/libclient.so: libhttp.so: cannot open shared object file: No such file or directory
When I put libhttp.so into /usr/lib64 the second way works fine.
Why libclient.so is looking for libhttp.so in /usr/lib64 if I use System.loadLibrary? How can I fix it without coping libhttp.so into /usr/lib64?
My loading code:
//Try load from -Djava.library.path
boolean found = false;
String lib = "client";
try {
System.loadLibrary(lib);
found = true;
} catch (UnsatisfiedLinkError e) {
e.printStackTrace();
}
//Try load from LD_LIBRARY_PATH
if (!found) {
lib = "libclient.so";
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()) {
System.load(x.getAbsolutePath());
found = true;
break;
}
}
}
Additional information.
If I test libclient.so with ldd then I see: libhttp.so => not found If I set export LD_LIBRARY_PATH = /home/client/lib64 then I see: libhttp.so => /home/client/lib64/libhttp.so