Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNI - UnsatisfiedLinkError - loadLibrary always fails

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?

like image 539
I82Much Avatar asked Dec 28 '22 08:12

I82Much


1 Answers

I'd check the following:

  1. 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?
  2. Is it working using java TranslatorWrapper -Djava.library.path=/path/to/example TranslatorWrapper
  3. 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
  4. If running Linux, have you tried appending /path/to/example/ to your LD_LIBRARY_PATH
like image 145
gawi Avatar answered Dec 31 '22 12:12

gawi