JNI_CreateJavaVM () fails every time I run my application (for sure)

I have a Windows MFC application that:

(1) Loads the JVM ( JNI_CreateJavaVM() )

(2) Attaches the main thread to the JVM ( AttachCurrentThread() )

(3) Loads some Java classes and methods ( FindClass() and GetMethodID() / GetStaticMethodID() )

(4) Registers some native callbacks for use by Java code ( RegisterNatives() )

(5) Separates the thread from the JVM ( DetachCurrentThread() )

(6) Destroys the JVM ( DestroyJavaVM() )

All of the above functions are executed every time I launch the application. I know that they succeed because, in addition to the above, I interact with the application and successfully call static Java methods, and these Java methods successfully call my own callbacks. My application exits gracefully, and it is sure that the expected Java functions and native callbacks are complete.

However, every time I run the application, the call to JNI_CreateJavaVM() fails (without populating JavaVM * ). Absolutely nothing changes between application runs . I just run it once (successfully, without even doing anything other than the above 6 steps), legally terminate, run it again, and it fails, back and forth. There are no exceptions for success and failure. I can run it dozens of times, and it oscillates at exactly another time between success and failure in the JNI_CreateJavaVM() .

If necessary, I will enclose more code. However, I hope someone has an idea of ​​what I have provided. (Note: this is the BCGSoft MFC property specification application, although I strongly doubt it is important.)

+8
java c ++ visual-studio visual-studio-2010 jni
source share
1 answer

It looks like you are working on this error (recounted here ), which will probably never be fixed.

Despite its name, DestroyJavaVM() does not actually destroy the JVM. What he does is signal the JVM that it should shut down, but the JVM actually waits until all threads other than the main thread stop before it actually shuts down. In fact, even then it is not completely cleaned up after itself, as the documentation says (rather cryptically ): "JDK / JRE still does not support VM unloading."

Also, I am concerned about your step 2: "Attaches the main thread to the JVM." You do not need to attach the thread that created the JVM for the JVM, and you cannot disconnect this thread. If you really do this, perhaps this is what messed up your system. (The thread that creates the JVM is the β€œMain” JVM thread. You only need to attach / disconnect other native threads to the JVM if they need access to it.)

By the way, JNI_CreateJavaVM() returns 0 on success, and you say that it returns 0 "unsuccessful" times, so in what sense is this an error? Which JVM (version, provider) are you using?

+4
source share

All Articles