Simple JNI:Calling Java From C/C++ >>
<< 女人们的进化史
Simple JNI: Calling C/C++ From Java

Author Zhou Renjian Create@ 2004-11-17 21:13
whizz Note icon

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.

本记录所在类别:
本记录相关记录: