Friday, February 02, 2007

creating jni binders

1. create the java stub


package example.jni;
public class HelloWorld {
private static native void writeHelloWorldToStdout();
public static void main(String[] args) {
System.loadLibrary("HelloWorld");
writeHelloWorldToStdout();
}
}


2.compile java class

javac example/jni/HelloWorld.java


3.create the header

javah -classpath c:/clay/projects/jnitest/src/java -o HelloWorld.h example.jni.HelloWorld


4. compile the c dll

gcc -mno-cygwin -I"/cygdrive/c/Program Files/Java/jdk1.5.0_08/include" -I"/cygdrive/c/Program Files/Java/jdk1.5.0_08/include/win32" -Wl,--add-stdcall-alias -shared -o ../../dll/HelloWorld.dll HelloWorld.c


5. run it

C:\clay\projects\jnitest\src\java>java -Djava.library.path=C:\clay\projects\jnitest\dll\ example.jni.HelloWorld

"Hello World!"

No comments: