头文件
#ifndef OBJECT_H
#define OBJECT_H
#include "jni.h"
class Object
{
public:
Object();
static bool BeginJVM();
static bool EndJVM();
protected:
static JNIEnv* env;
static JavaVM* jvm;
};
#endif // OBJECT_H
源文件:
#include "object.h"
//#include "JavaClasses.h"
#include "Object.h"
#include"QDebug"
#include"iostream"
#include"stdio.h"
using namespace std;
Object::Object()
{}
JNIEnv* Object::env=new JNIEnv;
JavaVM* Object::jvm=new JavaVM;
//创建JVM
bool Object::BeginJVM()
{
JavaVMOption options[3];
JavaVMInitArgs vm_args;
//各种参数
options[0].optionString = "-Djava.compiler=NONE";
options[1].optionString = "-Djava.class.path=.";
options[2].optionString = "-verbose:jni";//用于跟踪运行时地信息
// options[3].optionString = "-Djava.library.path=C:/Program Files/Java/jre7/bin/client";
vm_args.version=JNI_VERSION_1_1;
vm_args.options=options;
vm_args.nOptions=3;
vm_args.ignoreUnrecognized = JNI_TRUE;
qDebug("dddddd");
//创建JVM,获得jvm和env
cout<<&jvm<<" "<<(void **)&env<<" "<<&vm_args;
int res = JNI_CreateJavaVM(&jvm,(void **)&env, &vm_args);
qDebug(res+"");
return true;
}
bool Object::EndJVM()
{
//关闭JVM
jvm->DestroyJavaVM();
return true;
}
main文件:
#include <QCoreApplication>
#include"object.h"
#include"QDebug"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Object obj;
if(obj.BeginJVM())
qDebug("true");
return a.exec();
}