问题
I am attempting to get a simple JNI example working, but no matter what I do, I cannot get it to work using the loadLibrary command. It works perfectly if I specify the absolute path of the .so file and use System.load instead of System.loadLibrary.
Here is my directory tree:
.
|-- -
|-- TranslatorWrapper.c
|-- TranslatorWrapper.class
|-- TranslatorWrapper.cpp
|-- TranslatorWrapper.h
|-- TranslatorWrapper.java
`-- libTranslatorWrapper.so
Here is the Java code:
public class TranslatorWrapper {
public native String translate(byte[] bytes);
public static void main(String[] args) {
TranslatorWrapper w = new TranslatorWrapper();
System.out.println("From JNI: " + w.translate(null));
}
static {
System.out.println("Attempting to load library from " + System.getProperty("java.library.path"));
System.loadLibrary("TranslatorWrapper");
//System.load("/path/to/example/libTranslatorWrapper.so");
}
}
I know that the .so file needs to be in the java.library.path folder, so I start the program with the arguments
java TranslatorWrapper -Djava.library.path=.
since the library is found in the same directory as the .class file. However, it seems that the value is ignored:
Attempting to load library from .:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
Exception in thread "main" java.lang.UnsatisfiedLinkError: no TranslatorWrapper in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1754)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1045)
at TranslatorWrapper.<clinit>(TranslatorWrapper.java:14)
Note that the java.library.path variable has not been changed by my command line argument.
I also know that you call loadLibrary
with different arguments that you do load
(in particular, removing the lib prefix and the .so suffix); as you can see in the code I'm already doing that. Regardless of the fact that the .so file is in the current directory, that the current directory is on the java.library.path, and that I am calling the loadLibrary in the way I've seen it said online, none of this works.
Any idea what I'm doing wrong?
回答1:
I'd check the following:
- Are you sure that the current directory of the java process is the same as the *.so file? Sometime, a wrapper script can change that?
- Is it working using
java TranslatorWrapper -Djava.library.path=/path/to/example TranslatorWrapper
- If you are running Mac OS X, then see http://developer.apple.com/java/faq/development.html#anchor4 the file suffix should be .jnilib (or .dylib) and not .so
- If running Linux, have you tried appending
/path/to/example/
to your LD_LIBRARY_PATH
来源:https://stackoverflow.com/questions/3621158/jni-unsatisfiedlinkerror-loadlibrary-always-fails