代码之家  ›  专栏  ›  技术社区  ›  Grumblesaurus

在C++程序中指定路径

  •  2
  • Grumblesaurus  · 技术社区  · 7 年前

    jvm.dll 要在其%PATH%中执行。

    我想在代码中添加一行,告诉程序签入 <executable's directory>/jre/bin/server jvm.dll ,因此只需双击即可运行可执行文件。

    现在,当我运行它时,它说“程序无法启动,因为您的计算机中缺少jvm.dll。请尝试重新安装程序以解决此问题。”

    jre\bin\server 哪里 驻留并尝试运行它,它就工作了。 所以我只需要告诉我的程序在一个特定的其他地方搜索dll,我就可以开始了。

    HINSTANCE hVM = LoadLibrary("jre\\bin\\server\\jvm.dll"); . 不幸的是,这不起作用。

    我怎样才能实现这个目标?我不是C程序员或Windows程序员,如果问题是基本的,我道歉。

    #include <jni.h> 
    #include <windows.h>
    
    int main() {
       HINSTANCE hVM = LoadLibrary("jre\\bin\\server\\jvm.dll"); //Does not work
    
       JavaVM *jvm; 
       JNIEnv *env;
       JavaVMInitArgs vm_args; 
       JavaVMOption* options = new JavaVMOption[1];
    
       int index = 0;
       options[index].optionString = (char *)"-Djava.class.path=./main.jar";
    
       vm_args.version = JNI_VERSION_10;
       vm_args.nOptions = 1;
       vm_args.options = options;
       vm_args.ignoreUnrecognized = false;
       JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
       delete options;
    
       jmethodID main = NULL;
       jclass cls = NULL;
    
       cls = env->FindClass("net/joshuad/test/Main");
       if(env->ExceptionCheck()) {  
          env->ExceptionDescribe();
          env->ExceptionClear();
       }
    
       if (cls != NULL) {
          main = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
       } else {
          printf("Unable to find the requested class\n");
       }
    
       if (main != NULL) {
          env->CallStaticVoidMethod( cls, main, " ");
       } else {
          printf("main method not found") ;
       }
    
       jvm->DestroyJavaVM();
       return 0;
    }
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Sankalp Srivastava    7 年前

    只有当您的程序写在包含jre文件夹的同一目录中时,给定的路径才会起作用。否则,您必须指定完整位置,例如“C:\\jdk\\jrebin\server\\jvm.dll”

        2
  •  0
  •   Grumblesaurus    7 年前

    我想出来了。为了以这种方式链接到嵌入式JRE,我们需要 link explicitly 而不是隐式的,所以我们的程序不会在运行之前尝试链接,然后调用 JNI_CreateJavaVM

    #include <jni.h> 
    #include <windows.h>
    
    typedef UINT (CALLBACK* JVMDLLFunction)( JavaVM**, void**, JavaVMInitArgs * );
    
        int main() {        
            HINSTANCE jvmDLL = LoadLibrary(".\\jre\\bin\\server\\jvm.dll");
    
            if ( !jvmDLL ) {
                 printf("failed to find jvm.dll at specified location, exiting.\n");
                 return 1;
            }
    
            JVMDLLFunction createJavaVMFunction = (JVMDLLFunction)GetProcAddress(jvmDLL, "JNI_CreateJavaVM");
    
            if ( ! createJavaVMFunction ) {
                 printf("Failed to get pointer to JNI_CreateJavaVM function from jvm.dll, exiting\n");
                 return 1;
            } 
    
            JavaVM *jvm; 
            JNIEnv *env;
            JavaVMInitArgs vm_args; 
            JavaVMOption* options = new JavaVMOption[1];
    
            int index = 0;
            options[index].optionString = (char *)"-Djava.class.path=./main.jar";
    
            vm_args.version = JNI_VERSION_10;
            vm_args.nOptions = 1;
            vm_args.options = options;
            vm_args.ignoreUnrecognized = false;
    
            createJavaVMFunction( &jvm, (void**)&env, &vm_args );
    
            //JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
            delete options;
    
            jmethodID main = NULL;
            jclass cls = NULL;
    
            cls = env->FindClass("net/joshuad/test/Main");
            if(env->ExceptionCheck()) {  
              env->ExceptionDescribe();
              env->ExceptionClear();
            }
    
            if (cls != NULL) {
              main = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
            } else {
              printf("Unable to find the requested class\n");
            }
    
            if (main != NULL) {
              env->CallStaticVoidMethod( cls, main, " ");
            } else {
              printf("main method not found") ;
            }
    
            jvm->DestroyJavaVM();
            return 0;
        }
    
    推荐文章