Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I'm trying to run an .exe. When it runs, it shows the following error.
Error occurred during initialization of VM
Unable to load native library: Can't find dependent libraries


What does this mean?
JNIEnv* create_vm(JavaVM** jvm)
{
//JavaVM* jvm; /* denotes a Java VM */
JNIEnv* env; /* pointer to native method interface */
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
JavaVMOption options;
options.optionString = "C:\\Users\\HP\\eclipse-workspace\\src\\main\\java";
/* JDK/JRE 6 VM initialization arguments */

vm_args.version = JNI_VERSION_1_8;
vm_args.nOptions = 1;
vm_args.options = &options;
vm_args.ignoreUnrecognized =0;
/* load and initialize a Java VM, return a JNI interface
* pointer in env */
int res= JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
/* invoke the Main.test method using the JNI */
// int ret=JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);
if (res < 0)
{
printf("Error code:%d\n", GetLastError());
}
/* invoke the Main.test method using the JNI */
return env;
}

int main()
{
JNIEnv* env;
JavaVM* jvm;
env = create_vm(&jvm);
if (env == NULL)
{
printf("Got env");
}
jclass cls = (*env)->FindClass(env, "Main");


//Obtaining Classes
cls = (*env)->FindClass(env, "Main");
if (cls != NULL)
{
printf("Found the class");
}
else
{
printf("\nUnable to find the requested class\n");
}
return 0;
}


What I have tried:

I have tried updating my windows. No use. I used dependency walker, it shows me the following error:
Error: At least one required implicit or forwarded dependency was not found.
Warning: At least one delay-load dependency module was not found

What should I do?
Posted
Updated 10-May-21 4:50am
v3
Comments
Richard MacCutchan 10-May-21 4:33am    
You really need to go to the link I gave you yesterday and study the JNI system in detail. Starting a JVM and running some Java code is (in theory) quite straightforward, but (in practice) can cause problems. And posting such a question without showing your code does not help us to help you.
[no name] 10-May-21 8:23am    
Same code from yesterday(updated above). The code runs fine and .exe got created.
When I try to open my .exe, it shows the above error. Can u mention the dependencies needed to run this.
[no name] 10-May-21 9:08am    
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) Client VM (build 25.201-b09, mixed mode)
Richard MacCutchan 10-May-21 10:20am    
Sorry, my mistake, I was trying to build it as a C++ program, but it is C code. I will have another try.

If you don't understand an error message, Google it: Error occurred during initialization of VM Unable to load native library: Can't find dependent libraries - Google Search[^]
What it means is that a DLL is missing, or incompatible with the current version of the Java virtual machine.

We can't solve this for you (it needs your whole system and we don't have any access to that): follow a few of the links and find out exactly what the situation is.
 
Share this answer
 
OK, I have a working version, which required a couple of minor changes:
C++
#include <jni.h>

JNIEnv* create_vm(JavaVM** jvm)
{
    //JavaVM* jvm; /* denotes a Java VM */
    JNIEnv* env; /* pointer to native method interface */
    JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
    JavaVMOption options;

    // fix the following line as indicated
    options.optionString = "-Djava.class.path=<**add the path to your java program here**>";

    /* JDK/JRE 6 VM initialization arguments */

    vm_args.version = JNI_VERSION_1_8;
    vm_args.nOptions = 1;
    vm_args.options = &options;
    vm_args.ignoreUnrecognized =0;

    /* load and initialize a Java VM, return a JNI interface
    * pointer in env */
    int res= JNI_CreateJavaVM(jvm, (void**)&env, &vm_args); // no need for & on jvm

    if (res < 0)
    {
        printf("Error code:%d\n", res);
    }
    return env;
}

int main()
{
    JNIEnv* env;
    JavaVM* jvm;
    env = create_vm(&jvm);
    if (env == NULL)
    {
        printf("Failed to get env"); // corrected the error message
        return 1;
    }

    //Obtaining Classes
    jclass cls = (*env)->FindClass(env, "Main"); // Needs a Main.class object in the path listed above
    if (cls != NULL)
    {
        printf("Found the class");
        jmethodID jmid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V"); // See note below
        if (jmid != NULL)
        {
            (*env)->CallStaticVoidMethod(env, cls, jmid, NULL);
            printf("env->CallStaticVoidMethod returned\n");

        }
    }
    else
    {
        printf("\nUnable to find the requested class\n");
    }

    return 0;
}

GetStaticMethodID requires the name and signature of the method to search for. The signature is correct for the sample below.

That works with the following Java program whose class file is stored in the location indicated by the path set in the options.optionString above.
Java
import java.io.*;

@SuppressWarnings("unused")
class Main {

    public static void main(String[] argv) {
        System.out.println("This message should appear on the console");
    }
}
 
Share this answer
 
Comments
[no name] 10-May-21 11:19am    
Tried this too. Still I'm getting the same error:(
Error msg:

Error occurred during initialization of VM
Unable to load native library: Can't find dependent libraries
Richard MacCutchan 10-May-21 11:44am    
That probably means that your PATH environment variable is missing something. It needs to include the paths of the jvm.dll and any other Java support libraries. On my system it is:
jvm.dll is at
C:\Program Files\Java\jdk-14.0.1\bin\server

other dlls are at 
C:\Program Files\Java\jdk-14.0.1\bin
[no name] 10-May-21 12:25pm    
Still the same:( Do you believe it has something to do with java version. I have set my path to the dlls. I don't know why this is even happening.
Richard MacCutchan 10-May-21 14:56pm    
Sorry, it is impossible to guess from here. What java version are you using, have you checked that all the dlls are in the correct locations? What is the content of the PATH variable when you run the code? Are you running the test in VS2019 or in a command window?
[no name] 12-May-21 1:46am    
java version "1.8.0_201"
I have linked the dlls in correct position.
My Path variable: (in this order)
C:\Program Files (x86)\Java\jdk1.8.0_201\bin
C:\Program Files (x86)\Java\jdk1.8.0_201\jre\bin
C:\Program Files (x86)\Java\jdk1.8.0_201\jre\bin\server
I'm using VS2019.

I have tried implementing in another system too. But still the same error.I think I'm missing something.
I'm using Debug mode, X86 in VS2019. (Also tried in Release mode). I have changed the environment variables in every possible way. I updated the device (thrice). Still this error won't seem to go.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900