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!"

cigwin/x

two points to remember:

1. Cygwin/X is installed via Cygwin's setup.exe and the installation process is documented in the Cygwin/X User's Guide. Whether or not you already have Cygwin installed, you can add Cygwin/X to your installation by downloading the latest setup.exe, running setup, and selecting the 'xorg-x11-base' package from the 'X11' category.

2. The X server, XWin, can be launched in several ways. One of these is by running c:\cygwin\usr\X11R6\bin\startxwin.bat (you may wish to create a desktop shortcut to this batch file so that you can launch the X server by clicking on its icon). Another way to launch the X server is by running the command startx in a Cygwin window.