JNA unsatisfiedlinkerror – when I put Java library. Takes effect when path is set to a dummy value
Using JNA 4.0.0 on Linux 0, I tried to load a native library (libmean. So), which is located in the Lib subdirectory (this library is just a simple example of calculating the average of two numbers)
I run the following code (in eclipse) and set - djna. In the run configuration library. path = lib.
import com.sun.jna.Library; import com.sun.jna.Native; public class Mean { public interface MeanLib extends Library { MeanLib INSTANCE = (MeanLib) Native.loadLibrary("mean",MeanLib.class); double mean(double a,double b); } public static void main(String[] args) { double result = MeanLib.INSTANCE.mean(1.0,3.0); System.out.println(result); } }
However, this failed with the following exceptions:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Can't obtain updateLastError method for class com.sun.jna.Native at com.sun.jna.Native.initIDs(Native Method) at com.sun.jna.Native.<clinit>(Native.java:139) at com.sun.jna.examples.Mean$MeanLib.<clinit>(Mean.java:64) at com.sun.jna.examples.Mean.main(Mean.java:72)
Through trial and error, I found that if I also set up Java library. Path, the code will begin to work
However, this property works regardless of its value For example, I can set - DJava library. Path = XXXXXXXX and continue working Null values are also valid
What the hell is going on?
Solution
The fundamental problem is that the old version of JNA is installed on the system:
$dpkg -l | grep -i jna ii libjna-java 3.2.7-4 Dynamic access of native libraries from Java without JNI
JNA starts by trying to load its boot native library It searches for it everywhere, as described in documentation
By using - djna Nosys = true flag to solve this problem, which forces JNA from JNA Jar loads the native library, not from the system
Add Java library. Setting path to a meaningless value has a similar side effect - it overrides normal Java library. Path, which prevents loading the system version of JNA and starting from the local JNA Jar return version
Debug settings - djna debug_ Load = true is also useful for diagnosing JNA problems