Search code examples
c++windowsjava-native-interfacewindows-10git-bash

error while loading shared libraries: jvm.dll


I am trying to run a java program using c++ code. I tried below code

#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\n", str);                                             

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

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

I used below command to compile the code

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

It compiles fine, but when i run the executable like below, i am facing error

./a.exe

Error

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

Any Idea why this jvm.dll is not getting loaded?

PS: I am compiling and running from Git-Bash on windows 10.


Solution

  • It looks like your jvm.dll can not be found.

    Let's say we have 32bit MinGW installation (this is the version I have).

    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();
    }
    
    

    compilation and execution

    > 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
    

    you have to make sure that jvm.dll is visible on %PATH% - $PATH inside git-bash.