处理 JNI 崩溃
我有一个包含遗留 C 代码的 dll,我通过 JNI 调用此 dll,但有时 C 代码崩溃并导致 JVM 终止。有没有办法避免 JVM 崩溃?我可以处理 JNI 故障并让 JVM 生存吗? :)
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? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Timo 所说,除了使 JNI 代码健壮之外,您别无选择。如果您不能这样做(例如,如果您没有源代码),那么也许您可以寻求进程间解决方案。将 JNI 代码放在单独的服务器进程中,进行 RMI 或 HTTP 调用。如果崩溃,请重新启动“服务器”,但主 JVM 仍然存在。显然,这会带来性能开销,并且复杂性也会增加,但也许您能负担得起?
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?
如果是 C 代码崩溃,那么防止它带走 JVM 的唯一方法就是首先防止 C 代码崩溃。这是编写 JNI 代码时遇到的主要危险/问题之一,因为与纯 Java 编写的代码相比,它使 Java 和 C 的组合更加脆弱。
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.