第一步:编写Java端代码
定义一个Java类
public class TestNativeDemo {
// 声明本地方法
public native String testJni(String arg);
static {
// 加载DLL文件
System.loadLibrary("TestNativeDemoCPP");
}
public static void main(String args[]) {
TestNativeDemo ob = new TestNativeDemo();
// 调用本地方法
String result = ob.testJni("Hello,Jni"); // call a native method
System.out.println("TestNativeDemo.testJni=" + result);
}
}
----------------------------------------------
编译之后在生成TestNativeDemo.class的bin目录下执行javah TestNativeDemo命令生成头文件TestNativeDemo.h
----------------------------------------------
/* DO NOT EDIT THIS FILE - it is machine generated */sky7034
#include
/* Header for class TestNativeDemo */
#ifndef _Included_TestNativeDemo //避免重复包含头文件
#define _Included_TestNativeDemo
#ifdef __cplusplus //c++编译环境中才会定义__cplusplus
extern "C" { //告诉编译器下面的函数是c语言函数(因为c++和c语言对函数的编译转换不一样,主要是c++中存在重载)
#endif
/*
* Class: TestNativeDemo
* Method: testJni
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_TestNativeDemo_testJni
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
posted on 2012-01-12 09:43 墙头草 阅读(559) 评论(0) 编辑 收藏