How to unload the JVM from the life process?

I work with JNI and try to unload (destroy) the virtual machine using the DestoryJavaVM function (first I call the DetachCurrentThread method). It looks like this is now affecting the virtual machine, and it still remains after the call. I read in old Sun posts that DestoryJavaVM had problems in the past (JDK1.1-1.3 in 2001), but I'm using JRE 6, and it probably should work now, right? I need to load \ Unload a virtual machine into the same life process, since each boot requires different classes to boot. Any ideas how to do this?

Additional Information:

At the unloading stage, I can successfully disconnect CurrentThread and destroyVM (both return JNI_OK). I even FreeLibray (jvm.dll) successfully (return 1). When I try to load the JVM again, I can load LoadLibrary () and then find the CreateVM function in the DLL, and the CreateVM call will fail (return -1). What am I doing wrong here?

Thanks Guy

+4
source share
3 answers

Although he would not answer your question about DestroyJavaVM.

OSGi comes to my mind that you can put all the classes in a package, activate its code execution and deactivate it, later you use a different set. See Apache Felix .

Another less elegant option would be to exit vm and restart it using a different class path.

+2
source

You can check for erroneous threads. Invocation API: Virtual Machine Unloading mentions: "The VM waits until the current thread becomes the only non-daemon user thread before it actually unloads." This is required by the Java Language Specification, 12.8 .

+1
source

DestroyJavaVM() does not support unloading VMs.

DestroyJavaVM

Unloads the Java virtual machine and restores its resources.

Any stream, regardless of whether it is connected or not, can call this function. If the current thread is connected, the virtual machine waits until the current thread becomes the only non-daemon at the Java level. If the current thread is not connected, the virtual machine attaches the current thread, and then waits until the current thread becomes the only user-level thread without a daemon.

[...]

VM unloading is not supported.

The documentation may seem a bit controversial at first. What you can do is destroy your virtual machine without any problems, but since it is not properly unloaded, you can never restart it again in the same process.

0
source

Source: https://habr.com/ru/post/1312001/


All Articles