1

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?

Community
  • 1
  • 1
Igino Boffa
  • 411
  • 2
  • 6
  • 26
  • 1
    http://stackoverflow.com/questions/957700/how-to-set-the-java-library-path-from-eclipse this may help you – bichito Feb 03 '17 at 16:46
  • doesn't seem to work – Igino Boffa Feb 03 '17 at 16:49
  • First do you have an eclipse project in place? – bichito Feb 03 '17 at 16:53
  • yes I do... Also I tried, in another project to, print out `System.getProperty("java.library.path")` after using what is suggested in the answer you signaled, and it actually shows my path. Anyway, I cannot load the library I need – Igino Boffa Feb 03 '17 at 16:56
  • 1
    Did you check it is there? Maybe run the jvm in verbose mode to see if a clue pops up. Even check if the directories can be reached from the working directory? – bichito Feb 03 '17 at 16:58
  • Take a look here: https://github.com/mkowsiak/jnicookbook/tree/master/recipeNo023 This is exactly the case as you have. Where shared lib is required by JNI code. Maybe it will help (unfortunately, no Eclipse - but maybe it will shade some light on the issue) – Oo.oO Feb 04 '17 at 14:41
  • @mko I have already done that. The fact here is that the shared lib I call with JNI has dependencies on other shared libraries – Igino Boffa Feb 06 '17 at 08:31
  • @IginoBoffa one more question. Can you run "ldd GMApiJNI64.so" - maybe you miss some libs. Take a look at the outcome and double check you have all libs that are required by GMApiJNI64.so. – Oo.oO Feb 06 '17 at 14:59

1 Answers1

3

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

Oo.oO
  • 12,464
  • 3
  • 23
  • 45