4
votes

I'm a newbie to JNI, so I was trying this introduction to JNI tutorial earlier that just calls native to print Hello World! Everything went fine until the point that I wanted to run the java file, at which I keep getting the error: Exception in thread "main": java.lang.UnsatisfiedLinkError: no hello library found in java.library.path. I have googled the error and looked at a lot of peoples' suggestions, but none worked for me unfortunately! I have tried the following:

  • Running with command: java -Djava.library.path = "Path to library" HelloWorld
  • setting the LD_LIBRARY_PATH to my .so path

Everyone else had their issues resolved after doing one of the two above, but not me!

Here is the Java Code:

public class HelloWorld {

    static {
        System.loadLibrary("hello");
    }

    private native void printHelloWorld();

    public static void main(String[] args) {
        new HelloWorld().printHelloWorld();
    }
} 

And code for native is as follows:

void JNICALL Java_printHelloWorld(JNIEnv *env, jobject obj) {
    printf("HelloWorld!");
}

EDIT: I even tried copying the library to the actually directory of java.library.path, but it's still giving me the same error!

2
Please show your full code, for both the native part and the Java part. - merlin2011
java.library.path and LD_LIBRARY_PATH have to point to directories containing the .so file(s), not the file(s) themselve(s). - bmargulies
for me, 32 bit jvm gives the same message when the path contains 64 bit libraries... - 4esn0k

2 Answers

4
votes

What is your library called? If your paths are correct, your library name is probably wrong. On Windows the file needs to be called hello.dll, OS X (Java < 1.7) libhello.jnilib, OS X (Java >= 1.7) libhello.dylib and just about everything else will be libhello.so. Notice that the Windows dll file is the only file name without the "lib" prefix and that the "lib" prefix is not used when calling System.loadLibrary("hello"). If you are still experiencing a problem loading the lib, try System.load("/path/to/my/libhello.so") to try and load the library directly.

2
votes

@Alex Barker The version

System.load("/path/to/my/libhello.so")

does not resolve dependencies. If there are dependencies upon other user-defined libraries, they need to be loaded before.