源码部分参考了:http://public0821.iteye.com/blog/423941
当然,也改动了一些关键的地方,才让程序顺利调通。
不过调试的过程中也引出了好多问题,花了不少时间终于解决了,特此记录下整个解决问题的过程分享果实~^_^。
1.Java SDK安装路径:
操作系统:Windows 7 x64
C:\Program Files\Java\jdk1.7.0_15
C:\Program Files (x86)\Java\jdk1.6.0_17
2. 用eclipse建立一个Java工程叫JniTest1
假设路径在:D:\sonikk\project\java\workspace\JniTest1
文目录的放置关系:
JniTest1
__ src
__ __ test
__ __ __Demo.java
下面是Demo.java:
package test; /** * 该类是为了演示JNI如何访问各种对象属性等 */ public class Demo { //用于演示如何访问静态的基本类型属性 public static int COUNT = 8; //演示对象型属性 private String msg; private int[] counts; public Demo() { this("缺省构造函数"); } /** * 演示如何访问构造器 */ public Demo(String msg) { this.msg = msg; this.counts = null; } public String getMessage() { return msg; } /** * 该方法演示如何访问一个静态方法 */ public static String getHelloWorld() { return "Hello world!"; } /** * 该方法演示参数的传入传出及中文字符的处理 */ public String append(String str, int i) { return str + i; } /** * 演示数组对象的访问 */ public int[] getCounts() { return counts; } /** * 演示如何构造一个数组对象 */ public void setCounts(int[] counts) { this.counts = counts; } /** * 演示异常的捕捉 */ public void throwExcp()throws IllegalAccessException { throw new IllegalAccessException("exception occur."); } }
2.查看Signature,
将Demo.java在同一个目录下编译成.class文件:
这样最终的.class文件就会输出在如下路径:
D:\sonikk\project\java\workspace\JniTest1\src\test1\Demo.class
运行cmd:
# D:
# cd D:\sonikk\project\java\workspace\JniTest1\src\test
# javac Demo.java
# javap -s -p Demo
输出如下:
查看导出jvm.dll的函数:
找到VS2010的安装目录进入VC文件夹:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC
下面有一个文件叫vcvarsall.bat
打开cmd,将此文件拖入cmd当中,并单击回车,输出如下:
C:\Users\Administrator>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
接下来就可以进行dump 任意的DLL了,
# dumpbin /exports "C:\Program Files (x86)\Java\jdk1.6.0_17\jre\bin\client\jvm.dll"
# dumpbin /exports "C:\Program Files\Java\jdk1.7.0_15\jre\bin\server\jvm.dll"
这样可以知道jvm.dll当中有哪些函数~
当然,也可以不用这么麻烦,从VS2010的菜单也可以直接进行dump,操作方式:
Tools -> Visual studio 2010 Command Prompt
# cd d:
# dumpbin /exports "C:\Program Files (x86)\Java\jdk1.6.0_17\jre\bin\client\jvm.dll" /out:"C:\jvm_dump.txt"
结果就被重定向到了C:\jvm_dump.txt 中了,非常方便。
创建一个c++的console工程:
Configuration Properties -> C/C++ -> General -> Additional Include Directories:
C:\Program Files\Java\jdk1.7.0_15\include
C:\Program Files\Java\jdk1.7.0_15\include\win32
Configuration Properties -> Linker -> General -> Additional Library Directories:
C:\Program Files\Java\jdk1.7.0_15\lib
注意:使用1.7的64x jdk,调用
HINSTANCE hInstance = ::LoadLibrary(TEXT("C:\\Program Files\\Java\\jdk1.7.0_15\\jre\\bin\\client\\jvm.dll"));
会返回NULL,于是改用了32位的jdk 1.6版本,可以正常获取!
HINSTANCE hInstance = ::LoadLibrary(TEXT("C:\\Program Files (x86)\\Java\\jdk1.6.0_17\\jre\\bin\\client\\jvm.dll"));
出现错误:
error LNK2001: unresolved external symbol __imp__JNI_CreateJavaVM@12
正确的工程设置:
添加环境变量:
%JAVA_HOME% = C:\Program Files\Java\jdk1.7.0_15
Path: %JAVA_HOME%\jre\bin\server
Configuration Properties -> Project Defautls -> Character Set:
Not Set
Configuration Properties -> Debugging -> Working Directory:
$(OutDir)
Configuration Properties -> C/C++ -> General -> Additional Include Directories:
C:\Program Files (x86)\Java\jdk1.6.0_17\include
C:\Program Files (x86)\Java\jdk1.6.0_17\include\win32
Configuration Properties -> Linker -> General -> Additional Library Directories:
C:\Program Files (x86)\Java\jdk1.6.0_17\lib
下面上code实现c++调用java函数,实现字符串拼接( "sonikk小哥是"+250 )
注意:
classpath设置为: "D:\\sonikk\\project\\java\\workspace\\JniTest1\\src"
env->FindClass("test/Demo")才能刚好找到:D:\sonikk\project\java\workspace\JniTest1\src\test\Demo.class
cpp文件:
// JniTest1.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "windows.h" #include <string> #include <iostream> using namespace std; #include "jni.h" //#pragma comment(lib, "jvm.lib") jstring NewJString(JNIEnv *env, LPCTSTR str); string JStringToCString (JNIEnv *env, jstring str); // c++调用java int _tmain(int argc, _TCHAR* argv[]) { //定义一个函数指针,下面用来指向JVM中的JNI_CreateJavaVM函数 typedef jint (WINAPI *PFunCreateJavaVM)(JavaVM **, void **, void *); int res; JavaVMInitArgs vm_args; JavaVMOption options[3]; JavaVM *jvm; JNIEnv *env; /*设置初始化参数*/ //disable JIT,这是JNI文档中的解释,具体意义不是很清楚 ,能取哪些值也不清楚。 //从JNI文档里给的示例代码中搬过来的 options[0].optionString = "-Djava.compiler=NONE"; //设置classpath,如果程序用到了第三方的JAR包,也可以在这里面包含进来 options[1].optionString = "-Djava.class.path=.;D:\\sonikk\\project\\java\\workspace\\JniTest1\\src"; //设置显示消息的类型,取值有gc、class和jni,如果一次取多个的话值之间用逗号格开,如-verbose:gc,class //该参数可以用来观察C++调用JAVA的过程,设置该参数后,程序会在标准输出设备上打印调用的相关信息 options[2].optionString = "-verbose:NONE"; //设置版本号,版本号有JNI_VERSION_1_1,JNI_VERSION_1_2和JNI_VERSION_1_4 //选择一个根你安装的JRE版本最近的版本号即可,不过你的JRE版本一定要等于或者高于指定的版本号 vm_args.version = JNI_VERSION_1_4; vm_args.nOptions = 3; vm_args.options = options; //该参数指定是否忽略非标准的参数,如果填JNI_FLASE,当遇到非标准参数时,JNI_CreateJavaVM会返回JNI_ERR vm_args.ignoreUnrecognized = JNI_TRUE; //加载JVM.DLL动态库 HINSTANCE hInstance = ::LoadLibrary(TEXT("C:\\Program Files (x86)\\Java\\jdk1.6.0_17\\jre\\bin\\client\\jvm.dll")); if (hInstance == NULL) { return false; } //取得里面的JNI_CreateJavaVM函数指针 PFunCreateJavaVM funCreateJavaVM = (PFunCreateJavaVM)::GetProcAddress(hInstance, "JNI_CreateJavaVM"); //调用JNI_CreateJavaVM创建虚拟机 res = (*funCreateJavaVM)(&jvm, (void**)&env, &vm_args); // 添加jvm.lib引用 //res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); if (res < 0) { return -1; } //查找test.Demo类,返回JAVA类的CLASS对象 jclass cls = env->FindClass("test/Demo"); //根据类的CLASS对象获取该类的实例 jobject obj = env->AllocObject(cls); //获取类中的方法,最后一个参数是方法的签名,通过javap -s -p 文件名可以获得 jmethodID mid = env->GetMethodID(cls, "append","(Ljava/lang/String;I)Ljava/lang/String;"); //构造参数并调用对象的方法 const char szTest[] = "sonikk小哥是"; jstring arg = NewJString(env, szTest); jstring msg = (jstring) env->CallObjectMethod(obj, mid, arg, 250); cout<<JStringToCString(env, msg); //销毁虚拟机并释放动态库 jvm->DestroyJavaVM(); ::FreeLibrary(hInstance); printf("\nfinished...\n"); getchar(); return 0; } jstring NewJString(JNIEnv *env, LPCTSTR str) { if(!env || !str) { return 0; } int slen = strlen(str); jchar* buffer = new jchar[slen]; int len = MultiByteToWideChar(CP_ACP,0,str,strlen(str), (LPWSTR)buffer,slen); if(len>0 && len < slen) { buffer[len]=0; } jstring js = env->NewString(buffer,len); delete [] buffer; return js; } string JStringToCString (JNIEnv *env, jstring str) { if(str==NULL) { return ""; } //在VC中wchar_t是用来存储宽字节字符(UNICODE)的数据类型 int len = env->GetStringLength(str); wchar_t *w_buffer = new wchar_t[len+1]; char *c_buffer = new char[2*len+1]; ZeroMemory(w_buffer,(len+1)*sizeof(wchar_t)); //使用GetStringChars而不是GetStringUTFChars const jchar * jcharString = env->GetStringChars(str, 0); wcscpy(w_buffer, (wchar_t*)jcharString); env->ReleaseStringChars(str,jcharString); ZeroMemory(c_buffer,(2*len+1)*sizeof(char)); //调用字符编码转换函数(Win32 API)将UNICODE转为ASCII编码格式字符串 len = WideCharToMultiByte(CP_ACP,0,w_buffer,len,c_buffer,2*len,NULL,NULL); string cstr = c_buffer; delete[] w_buffer; delete[] c_buffer; return cstr; }
最后编译运行截图:
发现字符串被连起来了~OK!