http://www.cnblogs.com/liuling/p/2013-12-20.html
http://blog.csdn.net/cl05300629/article/details/39050703
1 创建Java包含native方法的类,如下:
public class TestNative { public native void sayHello(); }
2 通过JDK工具javah生成C/C++头文件,如下:
C:\Users\jacky_dai\Desktop\java>javah TestNative
3 生成如下的C++头文件,TestNative.h
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class TestNative */ #ifndef _Included_TestNative #define _Included_TestNative #ifdef __cplusplus extern "C" { #endif /* * Class: TestNative * Method: sayHello * Signature: ()V */ JNIEXPORT void JNICALL Java_TestNative_sayHello (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
4 创建C++ win32 DLL Project,并且包含java的include files并添加测试代码
#include <iostream> #include "TestNative.h" using namespace std; JNIEXPORT void JNICALL Java_TestNative_sayHello(JNIEnv *, jobject); { cout<<"hello world!"<<endl; }
5 添加Java测试代码
public class TestNative { public native void sayHello(); /** * @param args */ public static void main(String[] args) { System.loadLibrary("NativeCode"); TestNative tNative = new TestNative(); tNative.sayHello(); } }
6 编译运行demo,可以看见输出的“Hello World!”
javah TestNative.java java TestNative