C++通过JVM调用jar包

#include "jni.h"
#include "windows.h"
#include "stdafx.h"
typedef jint (WINAPI *_CreateJavaVM)(JavaVM **, void **, void *);
int _tmain(int argc, _TCHAR* argv[])
{
	int res;
	JavaVMInitArgs vm_args;
	JavaVMOption options[3];
	JavaVM *jvm;
	JNIEnv *env;
	/*设置初始化参数*/
	options[0].optionString = "-Djava.compiler=NONE"; //Disabled JIT
	options[1].optionString = "-Djava.class.path=.;hello.jar";
	options[2].optionString = "-verbose:NONE";
	vm_args.version = JNI_VERSION_1_6;
	vm_args.nOptions = 3;
	vm_args.options = options;
	vm_args.ignoreUnrecognized = JNI_TRUE;
	// Load jvm.dll
	HINSTANCE hInstance = LoadLibrary(L"C://Program Files//Java//jre6//bin//client//jvm.dll");
	if (hInstance == NULL)
		return false;
	_CreateJavaVM m_CreateJavaVM = (_CreateJavaVM)GetProcAddress(hInstance, "JNI_CreateJavaVM");
	res = (*m_CreateJavaVM)(&jvm, (void**)&env, &vm_args);
	if (res < 0)
		return -1;
	// Find the class
	jclass HelloClass = env->FindClass("app/Hello");
	// Find the main method id
	jmethodID Hello_main = 
	env-> GetStaticMethodID(HelloClass, "main", "([Ljava/lang/String;)V");
	// Call the main method.
	env->CallStaticVoidMethod(HelloClass, Hello_main);
	jvm->DestroyJavaVM();
	FreeLibrary(hInstance);
	return 0;
}

你可能感兴趣的:(jvm,C++,jar)