如何正确地将共享库加载到 Eclipse 中
How to correctly load shared libraries into Eclipse
我在 linux 上使用 JNI,但在检索共享库时遇到问题。
特别是,我使用包含在单个 SO 文件中的本机函数,但它与其他 SO 文件(我放在同一目录中)有依赖关系。
现在,我正在使用 System.load(absolutePath)
加载主 SO,但出现此错误:
...GMApiJNI64.so: libgpc64.so: cannot open shared object file: No such file or directory
其中 GMApiJNI64.so
是我正在使用的主库
直到现在我尝试过:
Set LD_LIBRARY_PATH to the folder containing the .so files
Set the -Djava.library.path
property into eclipse.ini file
Doing what is specified here: How to set the java.library.path from Eclipse
我还能做什么?
只是一个简短的回顾。我不知道你的确切环境。但我可以重现并解决与您类似的问题:
> 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
现在,让我们看看库发生了什么
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)
它不在那里。我们可以做的是将它添加到 LD_LIBRARY_PATH
test@linux-875l:~/workspace/jnicookbook/recipes/recipeNo023> export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd`/lib
然后重试。有效。
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!
您可以在 Eclipse 中做的是在项目设置中提供您的库的位置:
Project -> Properties -> Java Build Path
-> Libraries -> JRE System Library
-> Native library location -> Edit...
-> External folder
更新:
如果您在 LD_LIBRARY_PATH 上没有 libgpc64.so,可能仍然存在问题。
您还可以尝试一件事。
在构建 GMAapiJNI64.so 时,尝试使用以下内容:
-Wl,-rpath=$LOCATION_OF_LIBGPC -lgpc64
这一次,即使您在 LD_LIBRARY_PATH
中没有库,Eclipse 也应该能够正确启动您的代码
我在 linux 上使用 JNI,但在检索共享库时遇到问题。
特别是,我使用包含在单个 SO 文件中的本机函数,但它与其他 SO 文件(我放在同一目录中)有依赖关系。
现在,我正在使用 System.load(absolutePath)
加载主 SO,但出现此错误:
...GMApiJNI64.so: libgpc64.so: cannot open shared object file: No such file or directory
其中 GMApiJNI64.so
是我正在使用的主库
直到现在我尝试过:
Set LD_LIBRARY_PATH to the folder containing the .so files
Set the
-Djava.library.path
property into eclipse.ini fileDoing what is specified here: How to set the java.library.path from Eclipse
我还能做什么?
只是一个简短的回顾。我不知道你的确切环境。但我可以重现并解决与您类似的问题:
> 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
现在,让我们看看库发生了什么
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)
它不在那里。我们可以做的是将它添加到 LD_LIBRARY_PATH
test@linux-875l:~/workspace/jnicookbook/recipes/recipeNo023> export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd`/lib
然后重试。有效。
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!
您可以在 Eclipse 中做的是在项目设置中提供您的库的位置:
Project -> Properties -> Java Build Path
-> Libraries -> JRE System Library
-> Native library location -> Edit...
-> External folder
更新:
如果您在 LD_LIBRARY_PATH 上没有 libgpc64.so,可能仍然存在问题。
您还可以尝试一件事。
在构建 GMAapiJNI64.so 时,尝试使用以下内容:
-Wl,-rpath=$LOCATION_OF_LIBGPC -lgpc64
这一次,即使您在 LD_LIBRARY_PATH
中没有库,Eclipse 也应该能够正确启动您的代码