Igino Boffa
Igino Boffa

Reputation: 451

How to correctly load shared libraries into Eclipse

I'm using JNI on linux and I'm having troubles to retrieve shared libraries.

In particular, I use native functions contained in a single SO file, but it has dependencies with other SO files (which I put in the same directory).

Now, I'm using System.load(absolutePath) to load the main SO, but I get this error:

...GMApiJNI64.so: libgpc64.so: cannot open shared object file: No such file or directory

where GMApiJNI64.so is the main library I'm using

Until now I tried to:

What else can I do?

Upvotes: 1

Views: 3764

Answers (1)

Oo.oO
Oo.oO

Reputation: 13375

Just a short recap. I don't know your exact env. but I can reproduce and fix the issue similar to yours:

> git clone https://github.com/mkowsiak/jnicookbook.git
> cd jnicookbook/recipes/recipeNo023
> make
> make test
/usr/lib64/jvm/java/bin/java -Djava.library.path=:./lib -cp target recipeNo023.HelloWorld
Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/test/workspace/jnicookbook/recipes/recipeNo023/lib/libHelloWorld.so: libAnotherFunction.so: cannot open shared object file: No such file or directory
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)
    at recipeNo023.HelloWorld.<clinit>(HelloWorld.java:11)
Makefile:14: recipe for target 'test' failed
make: *** [test] Error 1

Now, let's see what happens with the lib

test@linux-875l:~/workspace/jnicookbook/recipes/recipeNo023> ldd lib/libHelloWorld.so 
    linux-vdso.so.1 (0x00007ffd34936000)
    libAnotherFunction.so => not found
    libc.so.6 => /lib64/libc.so.6 (0x00007f470c182000)
    /lib64/ld-linux-x86-64.so.2 (0x0000556276681000)

It's not there. What we can do is to add it on the LD_LIBRARY_PATH

test@linux-875l:~/workspace/jnicookbook/recipes/recipeNo023> export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd`/lib

and try again. Works.

test@linux-875l:~/workspace/jnicookbook/recipes/recipeNo023> make test/usr/lib64/jvm/java/bin/java -Djava.library.path=:/home/test/workspace/jnicookbook/recipes/recipeNo023/lib:./lib -cp target recipeNo023.HelloWorld
library: :/home/test/workspace/jnicookbook/recipes/recipeNo023/lib:./lib
Hello world!
Hello from another function!

What you can do - in Eclipse - is to provide location of your libs inside project settings:

Project -> Properties -> Java Build Path 
    -> Libraries -> JRE System Library 
    -> Native library location -> Edit... 
    -> External folder

Update:

There still might be an issue if you don't have libgpc64.so on LD_LIBRARY_PATH.

There is one more thing you can try.

While building GMAapiJNI64.so, try to use following:

-Wl,-rpath=$LOCATION_OF_LIBGPC -lgpc64

This time, Eclipse should be able to properly start your code even though you don't have your lib in LD_LIBRARY_PATH

Upvotes: 3

Related Questions