views:

375

answers:

3
+6  Q: 

Handle a JNI crash

I have a dll that contains legacy C code, I call this dll via JNI, but sometimes the C code crashes and causes the JVM to terminate. Is it there a way to avoid JVM crash? Can I handle the JNI fault and let the JVM survive? :)

+3  A: 

If it's the C code that is crashing, then the only way to prevent it from taking the JVM with it is to prevent the C code from crashing in the first place. That's one of the main dangers/problems that you get when writing JNI code as it makes the combination of Java and C somewhat more fragile compared to something that is written in pure Java.

Timo Geusch
+3  A: 

As Timo has said, you have no real alternative but to make the JNI code robust. If you can't do that (for example if you don't have the source) then perhaps you could go for an inter-process solution. Put the JNI code in a separate server process, make RMI or HTTP calls across. If it crashes, restart the "server" but main JVM survives. Obviously there's a performance overhead, and an increase in complexity, but maybe you can afford this?

djna
A: 

Unfortunately the C code is embedded in the java gui, it paints some graphics on a dedicated canvas. It is legacy code, and in the future it will be rewritten in java. In the meanwhile I need to make the application fault tolerant, but it seems there is no way... Obviously we are making this legacy code more robust, but this is a lost war... :)

Farr
One account on SO wasn't enough for you?
Fredrik