7
votes

I have a library called HelloWorld.so and a program HelloWorld.java with this content:

class HelloWorld {
     private native void print();
     public static void main(String[] args) {
         new HelloWorld().print();
     }
     static {
         System.loadLibrary("HelloWorld");
     }
 }

Now when I try to run HelloWorld.java I get this error:

$ /usr/java1.4/bin/java HelloWorld
Exception in thread "main"
java.lang.UnsatisfiedLinkError: no HelloWorld in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1491)
        at java.lang.Runtime.loadLibrary0(Runtime.java:788)
        at java.lang.System.loadLibrary(System.java:834)
        at HelloWorld.<clinit>(HelloWorld.java:7)

Any tips?

4
If your using Linux(Ubuntu terminal), Then please have a look on saurabhsharma123k.blogspot.in/2017/07/…SAURABH_12

4 Answers

16
votes

I had this problem and fixed it by renaming my library to libHelloWorld.so and following Michael Myers's suggestion. I'm on Arch Linux 64-bit.

HelloWorld.c:

#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"

/* shamelessly stolen from the book 'The Java Native Interface: Programmer's
   Guide and Specification' */
JNIEXPORT void JNICALL
Java_HelloWorld_print (JNIEnv *env, jobject obj) {
    printf("Hello World!\n");
}

HelloWorld.java:

class HelloWorld {
     private native void print();
     public static void main(String[] args) {
         new HelloWorld().print();
     }
     static {
         System.loadLibrary("HelloWorld");
     }
 }

Building and testing:

$ javac HelloWorld.java
$ javah -classpath . HelloWorld
$ gcc -shared -fPIC -I $JAVA_HOME/include -I $JAVA_HOME/include/linux HelloWorld.c -o libHelloWorld.so
$ java -classpath . -Djava.library.path=. HelloWorld
Hello World!

tl;dr: put lib at the beginning of the library's filename

12
votes

I think some points are helpful when getting this error:

  1. Check consistency of function name in .c files and generated files (.h)
  2. Name of jni library based on OS. Ex: In HelloWorld.java,System.loadLibrary("HelloWorld");
    • Solaris: libHelloWorld.so
    • Linux: libHelloWorld.so
    • Win: HelloWorld.dll
    • Mac: libHelloWorld.jnilib
  3. When running, add -Djava.library.path=PATH. PATH to place where you put your jni library

Here is my reference: https://blogs.oracle.com/moonocean/entry/a_simple_example_of_jni

7
votes

Where is HelloWorld.so located? You probably need to specify its parent directory using the command-line parameter "-Djava.library.path".

For example, if it's in "/path/libs/HelloWorld.so", add -Djava.library.path=/path/libs as an option when invoking java. For instance, it's "-Djava.library.path=lib" on one of my projects.

Edit: Dan Dyer points out that the environment variable LD_LIBRARY_PATH also can be used for this.

3
votes

@mmyers Thank you for responding. We found out that all we had to do was change System.loadLibrary to System.load and pass the full path + filename as argument, worked like a charm.

Even before doing so, we tried using the "-D" parameter and setting LD_LIBRARY_PATH but we weren't successful.

Go figure! :)

Thanks again, Karen