/*
开发环境准备,如在VC6.0中
将目录C:\JDK\include和C:\JDK\include\win32加入到开发环境的Include Files目录中,同时将C:\JDK\lib目录添加到开发环境的Library Files目录中。
*/
//Sample.cpp
#include "windows.h"
#include "jni.h"
#include <string>
#include <iostream>
using namespace std;
jstring NewJString(JNIEnv *env, LPCTSTR str);
string JStringToCString (JNIEnv *env, jstring str);
int main()
{
//定义一个函数指针,指身JVM中的JNI_CreateJavaVM函数
typedef jint (WINAPI *PFunCreateJavaVM)(JavaVM **, void **, void *);
int res;
JavaVMInitArgs vm_args;
JavaVMOption options[3];
JavaVM *jvm;
JNIEnv *env;
/*设置初始化参数*/
options[0].optionString = "-Djava.compiler=NONE";
//设置Jar classpath,包含自己写好的备调用的java class
options[1].optionString = "-Djava.class.path=.;c:\\";
//设置显示消息的类型,debug信息
options[2].optionString = "-verbose:NONE";
//设置JRE版本号,低于或等于自己安装的JRE版本
vm_args.version = JNI_VERSION_1_4;
vm_args.nOptions = 3;
vm_args.options = options;
/*该参数指定是否忽略非标准的参数,如果填JNI_FALSE,当遇到非标准参数时,JNI_CreateJavaVM会返回JNI_ERR*/
vm_args.ignoreUnrecognized = JNI_TRUE;
//加载JVM.DLL动态库
HINSTANCE hInstance = ::LoadLibrary("D:\\Java\\jdk1.6.0_06\\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);
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[] = "Jayce";
jstring arg = NewJString(env, szTest);
jstring msg = (jstring) env->CallObjectMethod(obj, mid, arg, 12);
cout<<JStringToCString(env, msg);
//销毁虚拟机并释放动态库
jvm->DestroyJavaVM();
::FreeLibrary(hInstance);
return 0;
}
string JStringToCString (JNIEnv *env, jstring str)// (jstring str, LPTSTR desc, int desc_len)
{
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,jcharString);
env->ReleaseStringChars(str,jcharString);
ZeroMemory(c_buffer,(2*len+1)*sizeof(char));
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;
}
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),buffer,slen);
if(len>0 && len < slen)
{
buffer[len]=0;
}
jstring js = env->NewString(buffer,len);
delete [] buffer;
return js;
}
//java文件,在C盘建目录test,在test下建Demo.java ,以下是代码,在test下编译生成Demo.class
package test;
public class Demo
{
public static int COUNT = 8;
private String msg;
private int[] counts;
public Demo()
{
this("qqqqq");
}
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.");
}
}