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!