参考link
[url]http://www.cnblogs.com/liuling/p/2013-12-20.html[/url]
[url]http://blog.csdn.net/cl05300629/article/details/39050703[/url]
1 创建Java包含native方法的类,如下:
2 通过JDK工具javah生成C/C++头文件,如下:
3 生成如下的C++头文件,TestNative.h
4 创建C++ win32 DLL Project,并且包含java的include files并添加测试代码
[img]https://img-blog.csdn.net/20140904123640781?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2wwNTMwMDYyOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast[/img]
[img]https://img-blog.csdn.net/20140904123951644?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2wwNTMwMDYyOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast[/img]
5 添加Java测试代码
6 编译运行demo,可以看见输出的“Hello World!”
[url]http://www.cnblogs.com/liuling/p/2013-12-20.html[/url]
[url]http://blog.csdn.net/cl05300629/article/details/39050703[/url]
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并添加测试代码
[img]https://img-blog.csdn.net/20140904123640781?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2wwNTMwMDYyOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast[/img]
[img]https://img-blog.csdn.net/20140904123951644?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2wwNTMwMDYyOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast[/img]
#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