C++调用java的方法

// env.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <assert.h>
#include "jni.h"
using namespace std;
#pragma comment(lib,"jvm.lib")

int main(int argc, char* argv[])
{

	int res;
	JavaVM* jvm;
	JNIEnv* env;
	JavaVMInitArgs vm_args;
	JavaVMOption options[3];

	options[0].optionString = "-Djava.compiler=NONE";
	options[1].optionString = "-Djava.class.path=.;D:\\android\\eclipse4.2\\workspace\\jni\\bin\\"; // .. is specially for this project
	options[2].optionString = "-verbose:jni";
	vm_args.version = JNI_VERSION_1_4;
	vm_args.nOptions = 3;
	vm_args.options = options;
	vm_args.ignoreUnrecognized = JNI_TRUE;
	res = JNI_CreateJavaVM (& jvm, (void* * )& env, & vm_args);

	if (res < 0)  
	{  
		return -1;  
	}  


	if (res < 0) {
		fprintf (stderr, "Can't create Java VM\n");
		return 1;
	}
	jclass cls = env->FindClass ("cn/yws/jni/Main");
	assert (0 != cls);
	
	jmethodID mid = env->GetMethodID(cls,"sayHello","(Ljava/lang/String)V");
	jobject obj=env->NewObject(cls,mid);
	env->CallVoidMethod(obj,mid,"hehe");
	printf("ok");
	getchar();

	return 0;
}
http://public0821.iteye.com/blog/423941

你可能感兴趣的:(C++调用java的方法)