Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper shutdown of JVM when launching from C++

I'm launching JVM from C++ code via JNI. I have a problem that when just quitting my C++ process it seems some shutdown hooks from JVM are not run, and therefore some temp resources are still being around, that in my particular case prevents launching JVM next time I open a C++ process. I tried jvm->DestroyJavaVM(), but after all my process windows were closed, I still could see the process running. What's the best wait to ensure that the JVM is shut down properly when launched via JNI?

Thanks!

like image 737
Vyacheslav Avatar asked Nov 10 '22 17:11

Vyacheslav


1 Answers

First of all, jvm->DestroyJavaVM() won't return till all non-daemon jvm threads have stopped, it does nothing but waiting for them to stop, so you should stop them in java.

Secondly, System.exit will cause the whole process to be shut down.

So what you really need is check your java code that which thread is not stopped yet, for example the background message loop thread of the ui framework such as gwt or swing.

like image 170
kyriosli Avatar answered Nov 14 '22 21:11

kyriosli