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

error while loading shared libraries: jvm.dll(加载共享库时出错:jvm.dll)
本文介绍了加载共享库时出错: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上可见。

这篇关于加载共享库时出错:jvm.dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

C++ find method not working(C++查找方法不起作用)
How to determine the offset of an element of a tuple at compile time?(如何在编译时确定元组元素的偏移量?)
Error: control Reaches end of non void function(错误:控件已到达非无效函数的末尾)
Error: Jump to case label in switch statement(错误:跳转到SWITCH语句中的CASE标签)
Order of elements in set of pointers(指针集中元素的顺序)
Why is quot;using namespace X;quot; not allowed at class/struct level?(为什么不允许在类/结构级别使用命名空间X;quot;?)