Exception in thread "main" java.lang.UnsatisfiedLinkError (Library not found despite being in java.lang.path)

时间:2016-08-31 18:54:49

标签: java c++ java-native-interface openjdk

I am working on a 64bit Linux machine. I am trying to emulate a simple JNI tutorial given here. After I have prepared all the files, i.e. Sample1.java, Sample1.class, Sample1.h, Sample1.cpp and libSample1.so, and when I try to run the command

java Sample1

I get the following error.

Exception in thread "main" java.lang.UnsatisfiedLinkError: no libSample1 in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1889)
        at java.lang.Runtime.loadLibrary0(Runtime.java:849)
        at java.lang.System.loadLibrary(System.java:1088)
        at Sample1.main(Sample1.java:10)

The problem line of Sample1.java is given as;

System.loadLibrary("libSample1");

I have read a question similar to mine here and tried to take advice from one of its answers, i.e. added current directory to environment variable $LD_LIBRARY_PATH (current directory contains libSample1.so file) using;

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.

But this still doesn't help me. Even a call to java -XshowSettings:properties shows that java.library.path contains . as one of the search paths.

When I replace relative file path with absolute file path by replacing System.loadLibrary("libSample1") with System.load("<absolute-file-path>/libSample1.so");, this problem goes away. Can someone tell me how to make sure that java.library.path can find my shared library file?

0 个答案:

没有答案