Problem with 2nd call to JNI method CreateJavaVM

Posted on 2005-05-10
Last Modified: 2012-06-27
Hi all,
I have written a little MS Win32 DLL that makes use of some Java API I've packed into a jar using the JNI mechanism. I've released this DLL to a customer who has the requirement due to requests from other parts of his program to load and unload my DLL for each call. He's doing something like this:

(*proc)(...); // call my DLL method which will forward the request to a Java VM

In my DLL the public Java VM to use is searched in the registry etc. Then a call to the JNI method CreateJavaVM() is made. This call returns the address of the Java VM to use, some environment structures and sets the classpath extension in order to find my jar. Then the Java class and method are searched and finally called. After that I call DetachCurrentThread() (works without) and DestroyJavaVM().

This all works fine for the first time. But when executed the seconde time the call to CreateJavaVM() returns with JNI_ERR (-1).  Loading the DLL once, calling the Java method as often as desired and then freeing the DLL once works fine. But doing this the 2nd time leads to the same error.

Does anybody know what to do in order to enable my DLL to be loaded and freed as often as desired and still get the VM created successfully? This would be of great help for me. Any suggestions are welcome.

Thanx in advance

Question by:Miknosoft
    LVL 13

    Expert Comment

    Hi Miknosoft,

    I think u don't need to call this function twice, DLLs are loaded only once


    Author Comment

    Hi petmagdy,
    as I've written I cannot influence the way my DLL is processed. To do it as described is a request from the customer. Would be too much work to change the current behaviour of his application.
    Thankx anyway,
    LVL 13

    Accepted Solution


    Try this, in ur dll put a static flag that indicates that CreateJavaVM is already called so it will not be called again, is this fisible to you?


    Author Comment

    Hi petmagdy,

    your hint doesn't work because static areas are reset when the DLL is unloaded. Thus, the flag gets lost. Anyway, your hint was valuable. Now I use a little trick to avoid the unloading of my DLL: I load it once by myself and never unload it. So my static area stays intact and I don't have to load and unload the Java VM all the time, which after all, should lead to better response time, besides the fact, that it doesn't lead to the expected results.

    Though you didn't really give me the solution, I'll accept your answer!

    Thanks for your interest in my question.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Title # Comments Views Activity
    Fibonacci challenge 11 61
    ArrayLists in ArrayLists/HashMaps? 4 27
    wordcount challenge 11 46
    Starting to learn JAVA, 7 34
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now