Using JNA 4.0.0, on Linux, I am trying to load a native library (libmean.so
), which is in the lib
subdirectory (the library is just a trivial example that calculates the mean of two numbers).
I run the following code (within Eclipse), with -Djna.library.path=lib
set in the run configuration.
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);
}
}
But this fails with the following exception:
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 have discovered that the code starts working if I also set java.library.path
.
However, it works regardless of the value of this property. For example, I can set -Djava.library.path=xxxxxxx
and it continues to work. An empty value also works.
What is going on?