Java JNI: Unsatisfied Link Error in Library Path

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.

10 Replies to “Java JNI: Unsatisfied Link Error in Library Path”

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

  5. Thanks, that was very useful!

    The only different thing is that I had to add ‘lib’ to the name of the shared library file (I am using Ubuntu 9.10 Karmic Koala):

    g++ -shared HelloWorld.c -o libHelloWorld.so -I/usr/lib/jvm/default-java/include -I/usr/lib/jvm/default-java/include/linux

  6. hello sir…

    i have a dll that measure cpu usage and i hava the cpp file but my probleam is
    i dont no where i put the dll file and where should i put the cpp file befor use it in java program

    i will be glad if u tell me what should i do

    thank u

  7. Hi,

    Sadly, its been a long time since I coded C++ and JNI together and cant seem to remember this anymore.

    Sorry

  8. Hi! This is kind of off topic but I need some advice from
    an established blog. Is it very difficult to set up your
    own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about making my own but
    I’m not sure where to start. Do you have any ideas or suggestions? Appreciate it

Leave a Reply

Your email address will not be published. Required fields are marked *