Javah生成JNI头文件

来源:互联网 发布:淘宝运费险能退多少钱 编辑:程序博客网 时间:2024/06/05 18:51

转自:http://www.cnblogs.com/abinxm/archive/2011/01/19/1939099.html

参考:http://blog.csdn.net/boise/archive/2007/08/08/1731334.aspx

参考:http://setting.javaeye.com/blog/304594

 

在eclipse中建一项目,建一class

 

复制代码
 1 package esmart.colfile.parse;
 2 
 3 public class TestHello {
 4     static {
 5          System.loadLibrary("TestHello");
 6       }
 7      
 8       public static native void hello(String msg);
 9      
10       public static void main(String[] args) {
11         
12          hello("Hello,Kimm!");
13         
14       }
15 
复制代码

 

 

F5刷新一下eclipse项目,让他编译一下(其实保存时也就编译了)

找到项目类存放目录如 G:\work\eclipse\test\bin

找到javah.exe ,可以配置到环境变量 ,方便调用

在命令行中输入 

cd G:\work\eclipse\test\bin

cd g:

 

E:\tools\Java\jdk1.6.0_11\bin\javah.exe -classpath . -jni esmart.colfile.parse.TestHello

执行完成后在项目bin目录下就生成一个 esmart_colfile_parse_TestHello.h 文件,把这个文件给c++项目引用,并实现其中的方法

 

VS2008中 工具 - 选项 - 项目和解决方案 - VC++目录 要增加 E:\tools\Java\jdk1.6.0_11\include\win32 和 E:\tools\Java\jdk1.6.0_11\include 具体目录根据本地JDK安装情况


eclipse 项目右键 properties 里面 设置 java build path 中的 native library location 到 VC项目的发布目录 

 

VC中 

打开StdAfx.h文件,再最后面添加:

 

#include <jni.h>

 

#include "javah生成的.h"

cpp文件中实现 .h 中的方法

VC++项目现在基本都是UNICODE

复制代码
 1 JNIEXPORT void JNICALL Java_esmart_colfile_parse_TestHello_hello(JNIEnv * env, jclass obj, jstring jMsg , jint )
 2 {
 3     
 4        //const char *strMsgPtr = env->GetStringUTFChars( jMsg , 0);   
 5 
 6        const WCHAR *strMsgPtr = (WCHAR *)env->GetStringChars(jMsg , 0);
 7 
 8        MessageBox( 0, strMsgPtr,L"Message box from VC++ "0 );
 9  
10        env->ReleaseStringChars(jMsg, (jchar *)strMsgPtr);
11       // env->ReleaseStringUTFChars( jMsg, strMsgPtr); 
12  
13 }
复制代码

 


0 0
原创粉丝点击