I am trying to call a simple native method in Java from C++, to achieve this I do the following:
Create a simple class as shown below:
public class HelloNative{ public native void printString(String str); static{ System.out.println("Current Directory is: " + System.getProperty("user.dir")); System.load(System.getProperty("user.dir") + "/libhellonative.so"); } public static void main(String[] args){ System.out.println("Calling Native Libraray (libhellonative.so) method printString"); new HelloNative().printString("Message from Java to C"); } }
Create .h file for the native method using
javah -jni
which create the following declaration:JNIEXPORT void JNICALL Java_HelloNative_printString(JNIEnv *, jobject, jstring);
Implement the native function in
.cpp
file as:JNIEXPORT void JNICALL Java_HelloNative_printString(JNIEnv* jni_env, jobject java_obj, jstring msg){ printf("inside native method\n"); jboolean iscopy; const char *message = (jni_env)->GetStringUTFChars( msg, &iscopy); printf("%s", message); }
And finally create the
.so
file using:g++ HelloNative.cpp -o libhellonative.so -shared -Wl,-soname,libhellonative.so -static -lc -I /usr/lib/jvm/java-6-sun-1.6.0.26/include -I /usr/lib/jvm/java-6-sun-1.6.0.26/include/linux
But when I compile and run the .java file it's giving me Runtime Exception:
Current Directory is: /home/gmuhammad/Projects/test
Calling Native Libraray (libhellonative.so) method printString
Exception in thread "main" java.lang.UnsatisfiedLinkError: HelloNative.printString(Ljava/lang/String;)V
at HelloNative.printString(Native Method)
at HelloNative.main(HelloNative.java:16)