您的位置:首页 > 其它

JNI

2016-01-02 16:12 316 查看
JNI——Java Native Interface

新建java工程,使用eclipse

package jnitest;

public class JniTest {
	static{
		System.loadLibrary("xmjni");
	}
	
	public native void printHello();

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("hello word.");
		new JniTest().printHello();
	}

}


进入src目录,编译
javac JniTest.java

生成头文件
javah -jni jnitest.Jnitest

编写.cpp文件
#include <jni.h>
#include "jnitest_JniTest.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_jnitest_JniTest_printHello
  (JNIEnv *, jobject){
	printf("hello, i am jni, i am succeed.\n");
  }


生成dll
gcc -shared -o xmjni.dll jnitest_JniTest.cpp

拷贝dll到工程根目录下,eclipse,run
hello word.
hello, i am jni, i am succeed.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: