Java call from C ++: how to catch / detect a fatal JVM error?

I am developing a C ++ program (Win32, MS Visual Studio 2008) that creates a Java virtual machine through JNI, as described here . It works great for a long time, both with Java 6 and Java 7.

Today I installed a new version of JRE; something went wrong in the installer, and the JRE became corrupt. I noticed that my C ++ program does not start and does not display any warning messages. Debugging the program showed that it worked successfully before calling JNI_CreateJavaVM ; but a call to JNI_CreateJavaVM leads to the immediate termination of the program. No return values, error messages, nothing.

Yes, I know that I just need to reinstall the JRE. Nevertheless, I would like my C ++ program to be prepared for such a situation. If he cannot create a Java virtual machine, he should show the message "Please reinstall JRE." But I do not have the opportunity to show this message, because the whole program ends.

Is there a way to detect this type of error in the JRE or, more generally, in a third-party library? I tried using C ++ try/catch constructs, I tried using a function - nothing helps; the program disappears without causing any catches or signal handlers.

Is there any way to detect such a JRE error? Or: is there a way to reliably detect failure or termination inside a third-party library?

+4
source share
1 answer

If you are using Linux / Unix: this is what I usually start with:

 struct sigaction sa; sa.sa_handler = bt_sighandler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; sigaction(SIGSEGV, &sa, NULL); sigaction(SIGUSR1, &sa, NULL); 

If you use Microsoft C ++ for Windows: _try / _except usually does the trick. This is an MSVC extension (you can tell from double underscore) that extends the standard try / catch.

Try / catch catches C ++ exceptions, _try / _except will catch all other unhandled exceptions (COM, Win32, ...). For example, it will catch zero dereferencing, memory access issues that are likely to cause your failure. Read here

Otherwise, in widows, try SetUnhandledExceptionFilter

EDIT: Since the approach using SEH seems to fail, you can go up one level and use Vectored Exception Handling. According to this msdn blog post , VEH is registered for each process and it is checked before SEH.

If all else fails, do not despair :) you can still turn your application into a para debugger / debugee. This will give you complete control over the events, life and death of the opening process. This is usually not worth it, but it is an additional solution that I had to use in the past. It is also not as difficult as it might seem; if something else doesn’t work, let me know and I dig some old code.

+1
source

All Articles