标签:style blog color ar 使用 java for sp strong
1 public class testdll{ 2 static{ 3 System.loadLibrary("dllname");//不包含[.dll] 4 } 5 6 public native void sayHello(); 7 8 public static void main(String[] args){ 9 testdll test = new testdll(); 10 test.sayHello(); 11 } 12 }
2.生成头文件 testdll.h
1 /* DO NOT EDIT THIS FILE - it is machine generated */ 2 #include <jni.h> 3 /* Header for class testdll */ 4 5 #ifndef _Included_testdll 6 #define _Included_testdll 7 #ifdef __cplusplus 8 9 extern "C" { 10 #endif 11 /* 12 * Class: testdll 13 * Method: sayHello 14 * Signature: ()I 15 */ 16 17 JNIEXPORT void JNICALL Java_testdll_sayHello(JNIEnv *, jobject); 18 #ifdef __cplusplus 19 } 20 #endif 21 #endif
函数名有特定格式,不能随意修改,Java_class_method,而且只能由class类对象才能调用,因为JNI是通过类名来查找对应方法的。
1 #include <stdlib.h> 2 3 JNIEXPORT void JNICALL Java_testdll_sayHello 4 (JNIEnv *env, jobject obj){ 5 printf("hello world\n"); 6 }
4.编译生成dll文件
标签:style blog color ar 使用 java for sp strong
原文地址:http://www.cnblogs.com/dream2003/p/4063163.html