Java高级应用之JNI

16 篇文章 0 订阅
参考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方法的类,如下:

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值