加载共享库时出错:jvm.dll

我正在尝试使用C++代码运行Java程序。我尝试了以下代码

#include<jni.h>                                                          
#include<stdio.h>                                                        

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

    JavaVM* vm;                                                      
    JNIEnv* env;                                                     
    JavaVMInitArgs vm_args;                                          
    vm_args.version = JNI_VERSION_1_2;                               
    vm_args.nOptions = 0;                                            
    vm_args.ignoreUnrecognized = 1;                                  

    // Construct a VM                                                
    jint results = JNI_CreateJavaVM(&vm, (void**)& env, &vm_args);   

    // Construct a String                                            
    jstring jstr = env->NewStringUTF("Hello World");                 

    // First get the class that contains the method you need to call 
    jclass clazz = env->FindClass("java/lang/String");               

    // Get the method that you want to call                          
    jmethodID to_lower = env->GetMethodID(clazz, "toLowerCase",      
            "()Ljava/lang/String;");                                 
    // Call the method on the object                                 
    jobject result = env->CallObjectMethod(jstr, to_lower);          

    // Get a C-style string                                          
    const char* str = env->GetStringUTFChars((jstring)result, NULL); 

    printf("%s
", str);                                             

    // Clean up                                                      
    env->ReleaseStringUTFChars(jstr, str);                           

    // Shutdown the VM.                                              
    vm->DestroyJavaVM();
}                                                                 

我使用以下命令编译代码

g++ LoadJVM.c -I/c/apps64/Java/jdk-11.0.1/include -I/c/apps64/Java/jdk-11.0.1/include/win32 -L/c/apps64/Java/jdk-11.0.1/lib/ -ljvm

它编译得很好,但是当我像下面这样运行可执行文件时,我遇到错误

./a.exe

错误

error while loading shared libraries: jvm.dll: cannot open shared object file: No such file or directory

知道为什么没有加载此jvm.dll吗?

PS:我正在从Windows 10上的Git-Bash编译和运行。


解决方案

看起来找不到您的jvm.dll

假设我们安装了32位MinGW(这是我拥有的版本)。

simple.cc

#include<jni.h>
#include<stdio.h>

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

    JavaVM* vm;
    JNIEnv* env;
    JavaVMInitArgs vm_args;
    vm_args.version = JNI_VERSION_1_2;
    vm_args.nOptions = 0;
    vm_args.ignoreUnrecognized = 1;

    // Construct a VM
    jint results = JNI_CreateJavaVM(&vm, (void**)& env, &vm_args);

    printf("Hello");

    // Shutdown the VM.
    (*vm).DestroyJavaVM();
}

编译和执行

> export JAVA_HOME="/c/Program Files (x86)/Java/jdk1.8.0_211/"
> export PATH="/c/Program Files (x86)/Java/jdk1.8.0_211/jre/bin/server/":"$PATH"
> g++ -o simple simple.cc -I"$JAVA_HOME/include/" -I"$JAVA_HOME/include/win32/" -L"$JAVA_HOME/lib" -ljvm
> ./simple
Hello

您必须确保jvm.dll%PATH%-$PATH内部git-bash上可见。

相关文章