I'm working with JNI and trying to unload (destroy) the VM using DestoryJavaVM function (I first call DetachCurrentThread method). It seems like the it has now influence on the VM and it is still up 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 VM in the same living process since each loading requires another classes to load. Any ideas how it can be done?
Additional info:
At the unloading phase I can success开发者_如何学Gofully detachCurrentThread and destroyVM (both return JNI_OK). I even FreeLibray (jvm.dll) successfuly (return 1). When I try to Load the JVM again I can LoadLibrary(), then find the CreateVM function in the DLL and the call to CreateVM fails (return -1). What I'm doing wrong here?
Thanks, Guy
Although it wouldn't answer your question on DestroyJavaVM.
OSGi comes to my mind you could put all classes in a bundle activate it run code and deactivate it, later you use another bundle. See Apache Felix.
Another option less elegant would be to exit the vm and restart it with another classpath.
You might check for errant threads. The Invocation API: Unloading the VM mentions, "The VM waits until the current thread is the only non-daemon user thread before it actually unloads." This is required by the Java Language Specification, 12.8.
DestroyJavaVM()
doesn't support VM unloading.
DestroyJavaVM
Unloads a Java VM and reclaims its resources.
Any thread, whether attached or not, can invoke this function. If the current thread is attached, the VM waits until the current thread is the only non-daemon user-level Java thread. If the current thread is not attached, the VM attaches the current thread and then waits until the current thread is the only non-daemon user-level thread.
[...]
Unloading of the VM is not supported.
The documentation can seem a bit conflicting at first. What you can do is destroy your VM without problems, but since it doesn't unload properly you can never reload it again in the same process.
For any newbies visiting this question, refer Calling JNI_CreateJavaVM function twice
Short answer: You CANNOT create more than one JVM in a single process (this is by design).
精彩评论