在Win64下經過jni啓動jvm並運行java類: java
jdk: jdk7_45(x64) c++
vs2010 64位命令行工具, 用來編譯咱們寫的C++,就是這個: 程序員
環境變量設置: jvm
java代碼(待執行): 工具
public class HelloInvocation{ public static int add(int a, int b) { return a+b; } }c++代碼:
#include <jni.h> #include <stdlib.h> #include <string.h> #ifdef _WIN32 #define PATH_SEPARATOR ';' #else #define PATH_SEPARATOR ':' #endif int main(){ JavaVMOption options[1]; JNIEnv *env; JavaVM *jvm; JavaVMInitArgs vm_args; long status; jclass cls; jmethodID mid; jint result; options[0].optionString = "-Djava.class.path=."; memset(&vm_args, 0, sizeof(vm_args)); vm_args.version = JNI_VERSION_1_6; vm_args.nOptions = 1; vm_args.options = options; status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); if (status != JNI_ERR){ cls = env->FindClass("HelloInvocation"); if (cls != 0){ mid = env->GetStaticMethodID(cls, "add", "(II)I"); if (mid != 0){ result = env->CallStaticIntMethod(cls, mid, 4, 7); printf("Hi, 4 + 7 = %d\n", result); } } jvm->DestroyJavaVM(); return 0; } else{ return -1; } }做爲一個看到這裏的程序員,你應該很容易理解上面的代碼,我很少說;
拿出你的Vs2010命令行工具, 編譯吧, 運行吧(編譯要加入咱們java jvm的庫文件依賴哈): spa