I started looking into Java's JNI feature. I followed this [tutorial][1]. So my class goes like this :
package me.gagan.pheonix.natve;
public class HelloJNI {
static {
System.loadLibrary("hello");
}
private native void sayHello();
public static void main(String... args) {
new HelloJNI().sayHello();
}
}
HelloJNI.c file like this:
\#include <jni.h>
\#include <stdio.h>
\#include "me_gagan_pheonix_natve_HelloJNI.h"
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) {
printf("Hello World in native C!\n");
return;
}
My directory structure is:
<pre>
C:.
├───bin
│ ├───me
│ │ └───gagan
│ │ └───pheonix
│ │ └───natve
│ │ └───HelloJNI.class
│ ├───resources
│ │ └───hello.dll
│ ├───me_gagan_pheonix_natve_HelloJNI.h
│ └───HelloJNI.c
└───src
├───me
│ └───gagan
│ └───pheonix
│ └───natve
│ └───HelloJNI.java
└───resources
</pre>
I also added
-Djava.library.path=C:\Gagan\workspace\Pheonix\bin\resources
But still error is coming. Any idea what is going wrong. Followed each step correctly. Following commands worked for me
gcc -Wl,--add-stdcall-alias -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o hello.dll HelloJNI.c
echo %JAVA_HOME%
C:\Java\jdk1.6.0_45
javah -verbose -jni -classpath . me.gagan.pheonix.natve.HelloJNI
EDIT:
Exception encountered is :
Exception in thread "main" java.lang.UnsatisfiedLinkError: me.gagan.pheonix.natve.HelloJNI.sayHello()V
at me.gagan.pheonix.natve.HelloJNI.sayHello(Native Method)
at me.gagan.pheonix.natve.HelloJNI.main(HelloJNI.java:12)
[1]: https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html
Hi anybody having any suggestions ??
package me.gagan.pheonix.natve;
public class HelloJNI {
...
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) {
You've changed the package name after you generated the .h file with javah
. Do it again, and adjust the function name in the .c file accordingly, to agree with the declaration in the .h file.