4

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!

3
  • Please show your full code, for both the native part and the Java part. Commented Apr 5, 2014 at 22:42
  • 1
    java.library.path and LD_LIBRARY_PATH have to point to directories containing the .so file(s), not the file(s) themselve(s). Commented Apr 5, 2014 at 22:48
  • for me, 32 bit jvm gives the same message when the path contains 64 bit libraries... Commented Jun 4, 2019 at 13:17

2 Answers 2

5

What is your library called? If your paths are correct, your library name is probably wrong. If the library you are loading is called hello, on Windows the file needs to be called hello.dll, on every other SO you also need to prepend the lib prefix:

  • on OS X (Java < 1.7) libhello.jnilib
  • on OS X (Java >= 1.7) libhello.dylib
  • on 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.

You can always check the file the system will look for by running

System.mapLibraryName(libName)
Sign up to request clarification or add additional context in comments.

1 Comment

hint if someone is using Mac OS X 10.9 Mavericks and IntelliJ: install opencv with homebrew $ brew install homebrew/science/opencv --with-java add opencv to (global) libraries: /usr/local/Cellar/opencv/2.4.9/share/OpenCV/java edit run configuration (vm option): -Djava.library.path=/usr/local/Cellar/opencv/2.4.9/share/OpenCV/java Still getting an error like this? Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java249 in java.library.path check Java version: $ java -version java version "1.6.0_65" solution: upgrade the Java SDK
2

@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.

2 Comments

use ldd /path/to/my/libhello.so to see dependencies
+ On Windows, good old ´Depency Walker´ does the job

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.