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? :)
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?
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.
精彩评论