Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using this as a reference to call my java class inside C program. But , JVM is not launching. Here is my code for launching jvm.
<JNIEnv* create_vm(JavaVM** jvm)
{
    JNIEnv* env;
    JavaVMInitArgs vm_args;
    JavaVMOption options;
    HMODULE jvm_dll=NULL;
    int ret;

    options.optionString = "C:\\Test\\src\\main\\java"; //Path to the java source code
    vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
    vm_args.nOptions = 1;
    vm_args.options = &options;
    vm_args.ignoreUnrecognized = 0;
    char t[400];
    mbstowcs(t, "C:\\Program Files (x86)\\Java\\jdk-16.0.1\\jre\\bin\\server\\jvm.dll", 400);
    jvm_dll = LoadLibrary(t);

    /// You might check the GetLastError() here after the LoadLibrary()
    if (jvm_dll == NULL) {
        printf("can't load dll\n");
        printf("error code %d", GetLastError());
    }

    JNI_CreateJavaVM_ptr = (JNI_CreateJavaVM_func)GetProcAddress(jvm_dll, "JNI_CreateJavaVM");

    /// You might check the GetLastError() here
    if (JNI_CreateJavaVM_ptr == NULL) { printf("can't load function\n"); 
    printf("error code %d", GetLastError());
    }

    printf("came here");
    ret = JNI_CreateJavaVM_ptr(jvm, (void**)&env, &vm_args);
    if (ret < 0) 
    { 
    printf("\nUnable to Launch JVM\n");
    printf("error code %d", GetLastError());
    }
    else
    {
        printf("got it"); 
    }
    printf("end");
    return env;
}
int main()
{
    JNIEnv* env;
    JavaVM* jvm;
    env = create_vm(&jvm);
    if (env == NULL)
    {
        printf("Error");
    }
return 0;
}

It throws error 203. But, I have no idea what it means.

What I have tried:

I am using visual studio. I have added jvm.lib to the project properties and also in the path. I don't know if I'm loading the dll correctly. Help me to launch jvm.
Posted
Updated 9-May-21 1:14am
v2

1 solution

I do not know where you found that code but the official documentation is different: The Invocation API[^].

Also why do you write:
C++
mbstowcs(t, "C:\\Program Files (x86)\\Java\\jdk-16.0.1\\jre\\bin\\server\\jvm.dll", 400);
jvm_dll = LoadLibrary(t);

instead of:
C++
jvm_dll = LoadLibrary(L"C:\\Program Files (x86)\\Java\\jdk-16.0.1\\jre\\bin\\server\\jvm.dll");
// the L prefix generates a Unicode string.

But looking at the documentation on the above link you do not need LoadLibrary.
 
Share this answer
 
Comments
[no name] 9-May-21 7:20am    
The L prefix works for even C. I thought it only works for C++. Anyways, I'm still not able to launch JVM. It shows the same error code.
Also, I have tried the code in the link you have mentioned. It throws the following error: error LNK2019: unresolved external symbol __imp__JNI_CreateJavaVM@12 referenced in function _main
Richard MacCutchan 9-May-21 8:04am    
I created a simple test using the code from the documentation (link above) and that works fine.
[no name] 9-May-21 8:30am    
Still the same error. Can you please share your code here? I'm really clueless. Have been trying this past 2 days.
Richard MacCutchan 9-May-21 9:08am    
The only code I have is what I took from that link.
[no name] 9-May-21 8:08am    
I solved it. It runs fine. But if I run my .exe file, it shows the following prompt: "The code execution cannot proceed because jvm.dll was not found".

I have tried adding the jvm.dll to the project's library. Even added it as a resource file. Can you possibly help me with this?

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