UnsatisfiedLinkError while using Java Native Interface

Hi,

I am trying to build a simple program using Java Native Interface, but trying to execute the Java program gives me an error. I am giving the program and the procedure that I have used here.

public class MyClass {

        public static native void hello();

        public static void main (String args]) {
                MyClass m=new MyClass();
                m.hello();

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

Then, I have compiled this program file using the command javac MyClass.java.

I have created another C program file named Hello.c, as given below:

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

JNIEXPORT void JNICALL Java_MyClass_hello
  (JNIEnv *env, jclass jc)
  {
          printf("Hello World");
  }

Then, I have compiled this C program using the command:
gcc Hello.c -fPIC -I /usr/lib64/jvm/java-1.6.0-openjdk-1.6.0/include/linux/
-I /usr/lib64/jvm/java-1.6.0-openjdk-1.6.0/include/ -shared -o libMyClass.so

This compiled successfully without giving any errors. Now, I tried to run the program, using the command:
java -cp . MyClass

Exception in thread “main” java.lang.UnsatisfiedLinkError: no MyClass in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1681)
at java.lang.Runtime.loadLibrary0(Runtime.java:840)
at java.lang.System.loadLibrary(System.java:1047)
at MyClass.<clinit>(MyClass.java:34)
Could not find the main class: MyClass. Program will exit.

As for the version of the compiler, and the Virtual Machine, java -version gives me the following information:

java version “1.6.0_18”
OpenJDK Runtime Environment (IcedTea6 1.8.1) (suse-1.1.5-x86_64)
OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode)

I would be grateful if someone in this forum could help me on this. Thanks for reading this!

The native library is usually not searched for in the java class path (which
is as the name suggests for java classes not for native libraries) but it
searches some predefined paths and in the paths defined by the system
property java.library.path (also the error message tells you exactly that)

So try to start your program with the following command line


java -Djava.library.path=. -cp . MyClass

provided your libMyClass.so is really in the same folder


openSUSE 11.3 64 bit | Intel Core2 Quad Q8300@2.50GHz | KDE 4.5 | GeForce
9600 GT | 4GB Ram
openSUSE 11.3 64 bit | Intel Core2 Duo T9300@2.50GHz | KDE 4.5 | Quadro FX
3600M | 4GB Ram

Yes, that solved my problem! Thanks a lot, Martin_helm, your solution really helped, and I also understood the reasoning behind your solution. It really did the trick for me. Thank you once again for solving this problem! I am really grateful.

Glad to hear it solved the problem. Have much fun with java programming. :slight_smile:

There is one additional thing I do not undersatnd in your java code. You
declare the hello method as static but later you create an instance of the
class to call it

MyClass m=new MyClass();
m.hello();

instead of calling it static.
Why do you do that?


openSUSE 11.3 64 bit | Intel Core2 Quad Q8300@2.50GHz | KDE 4.5 | GeForce
9600 GT | 4GB Ram
openSUSE 11.3 64 bit | Intel Core2 Duo T9300@2.50GHz | KDE 4.5 | Quadro FX
3600M | 4GB Ram