在64位C ++中使用JNI时,从JAR加载类失败

时间:2019-01-28 16:22:18

标签: java c++ java-native-interface jnienv

我正在尝试使用V2017在64位C ++上使用JNI加载JAR文件中包含的类。我可以实例化JVM,但是JNI似乎无法找到该类。

我尝试将类路径设置为包含要查找的类的包含JAR的目录,并将其设置为JAR的完整路径,但这也不起作用,因此感谢您的任何建议。

int main()
{
   JavaVM *jvm;   /* denotes a Java VM */
   JNIEnv *env;  /* pointer to native method interface */
   JavaVMInitArgs vm_args; /* JDK/JRE 8 VM initialization arguments */
   JavaVMOption *options = new JavaVMOption[1];
   options[0].optionString = (char *)"-Djava.class.path=C:\\AP\\lib\\myjar.jar";

   HINSTANCE hinstLib;
   std::string result = DetectJavaVM();
   hinstLib = LoadLibrary(result.data());

   if (hinstLib == 0)
   {
      printf("Error");
   }

   if (hinstLib != NULL)
   {
      typedef jint(JNICALL *PtrCreateJavaVM)(JavaVM **, void **, void *);
      typedef jint(JNICALL *PtrGetDefaultJavaVMInitArgs)(void *);

      PtrGetDefaultJavaVMInitArgs ptrGetDefaultArgs = (PtrGetDefaultJavaVMInitArgs)GetProcAddress(hinstLib, "JNI_GetDefaultJavaVMInitArgs");
      ptrGetDefaultArgs(&vm_args);
      vm_args.version = JNI_VERSION_1_8;
      vm_args.options = options;
      vm_args.nOptions = 1;
      vm_args.ignoreUnrecognized = JNI_TRUE;

      PtrCreateJavaVM ptrCreateJavaVM = (PtrCreateJavaVM)GetProcAddress(hinstLib, "JNI_CreateJavaVM");

      int res = ptrCreateJavaVM(&jvm, (void**)&env, &vm_args);

      jclass cls = env->FindClass("org/mypackage/myclass");

      jmethodID mid;

      if (cls != 0)
      {
         mid = env->GetStaticMethodID(cls, "intMethod", "(I)I");

      }

  }

}

0 个答案:

没有答案