1. Write Sample1.java:
/**
* @author Janyckee Jozz
*/
public class Sample1 {
public native int getNumber();
public static void main(String[] args) {
System.loadLibrary("Sample1");
Sample1 sample = new Sample1();
int number = sample.getNumber();
System.out.println(number);
}
}
2. Command:
S:\tutorials\j-jni>javac Sample1.java
3. Command:
S:\tutorials\j-jni>javah -classpath . Sample1
4. Write Sample2.c:
#include "Sample1.h"
JNIEXPORT jint JNICALL Java_Sample1_getNumber(JNIEnv *env, jobject obj) {
return 99 * 99;
}
void main() {
}
5. Command:
(Maybe you need to setup some path for cl.exe)
S:\tutorials\j-jni>set LIB=E:\Program Files\Microsoft Visual Studio\VC98\Lib
6. Command:
S:\tutorials\j-jni>cl -I"E:\Program Files\j2sdk1.4.2_02\include" -I"E:\Program Files\j2sdk1.4.2_02\include\win32" -I"E:\Program Files\Microsoft Visual Studio\VC98\Include" -LD Sample1.c -FeSample1.dll
7. Command:
S:\tutorials\j-jni>java -cp . Sample1
9801
DONE!
Resources:
The tutorial "Java programming with JNI" (developerWorks, March 2002) covers the two most common applications of JNI: calling C/C++ code from Java programs, and calling Java code from C/C++ programs.