Java JNI: Unsatisfied Link Error in Library Path

Date May 5, 2009

I’m currently playing around with Java Native Interfaces(JNI) and I must say its been too long since I programmed in Java. Its good to be back! Anyway, lets hop to the main topic…

I was following the tutorial from Sun in creating my first JNI and calling my compiled C++ library, I was stuck in the last part where this error popped out whenever I tried to invoke

java HelloWorld

the error was…

java.lang.UnsatisfiedLinkError: no HelloWorld in java.library.path
          at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1709)
          at java.lang.Runtime.loadLibrary0(Runtime.java:823)
          at java.lang.System.loadLibrary(System.java:1030)
          at HelloWorld.<clinit>(HelloWorld.java:<line number>)
Could not find the main class: HelloWorld. Program will exit.

one way of fixing this is to include the current location of your newly compiled shared library (ex. HelloWorld.so). I’m using Ubuntu linux,so to include the current folder where your “.so” file is located, invoke…

LD_LIBRARY_PATH=`pwd`
export LD_LIBRARY_PATH

or you can also do …

LD_LIBRARY_PATH=.
export LD_LIBRARY_PATH

another way is to define the location when running the program…

java -Djava.library.path=. HelloWorld

this will instruct java to locate the native library in the current location. Now call your program again and this time it work execute your native program.

It’s possible that when you execute your program after doing the above steps, another error will come out. This happened to me and the culprit was when I compiled the cpp file. The error would look something like…

java.lang.UnsatisfiedLinkError: <library location>: <library location>: only ET_DYN and ET_EXEC can be loaded
        at java.lang.ClassLoader$NativeLibrary.load(Native Method)
        at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1778)
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1674)
        at java.lang.Runtime.load0(Runtime.java:770)
        at java.lang.System.load(System.java:1005)
        at HelloWorld.<clinit>(HelloWorld.java:16)

to fix this, include the “-shared” option when compiling the cpp file.

g++ -shared HelloWorld.cpp -o HelloWorld.so -I /path/to/your/java/include/folder/where/jni.h/is/located -I /path/to/your/java/include/linux/folder/

Do not include the “-c” option, this will skip the linking stage.

Related posts

4 Responses to “Java JNI: Unsatisfied Link Error in Library Path”

  1. Neth said:

    Hello,

    May I ask you about using Visual Studio c++ as well. As I have the same problem as those errors, I have been searching for the solution but none of them can fix the problem. What is the command in VS c that is equivalent to -shared in g++ that you used?

    Thanks in advance

  2. Neth said:

    Hello there,

    I am 3 days new about JNI. I have been trying to compile exactly the same HelloWorld as I saw from the book here http://java.sun.com/docs/books/jni/html/start.html#769. I follow the step and finally I got the error complaint like:

    java.lang.ClassLoader.loadLibrary0(Unknown Source)
    java.lang.ClassLoader.loadLibrary0(Unknown Source)
    java.lang.Runtime.loadLibrary0(Unknown Source)
    java.lang.System.loadLibrary(Unknown Source)
    HelloWorld.(clinit)(HelloWorld.java:7)

    Anyone knows what is the equivalent command in MS VS c++ as -shared in dev-c++? Please I am really stuck with that for 2 days.

  3. Chaoz said:

    Hi Neth,

    I’m not so sure about MS VS C++ since its been years since I developed in that environment. But may i suggest looking for an option where you can add parameters during compilation?

    thanks

  4. Neth said:

    Hey! but fortunately I just got a solution last night. Here is my command lines used to compile my dll:

    C:\Program Files\Microsoft Visual Studio 9.0\VC\bin>cl -I”C:\Program Files\Java\include” -I”C:\Program Files\Java\include\win32″ -MD -LD HelloWorld.c -FeHelloWorld.dll

    There will be so many errors as my previous comment although, w/o the command -MD makes all errors disappeared and output as “Hello World!” perfectly after taking the command >java HelloWorld to run everything altogether.

    BTW, yes you should if you know the detail about these parameters it is used d/r compilation. Because as far as I know -MD option ensures that HelloWorld.dll is linked with the Win32 multithreaded C library. The -LD option instructs the C compiler to generate a DLL instead of a regular Win32 executable.

    So, I am wondering about such a strange behavior of -MD command now.

    Thanks for your suggestion

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">