如何在从JNI调用的代码中捕获System.exit() [英] How to trap System.exit() in code called from JNI

查看:121
本文介绍了如何在从JNI调用的代码中捕获System.exit()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C接口到调用System.exit()的java库中.我打电话给:

I'm writing a C interface to a java library that calls System.exit(). I call:

/* Calls the main method for the class */
printf("about to call main\n");
(*env)->CallStaticVoidMethod(env, mainClass, mainMethod, args);
printf("returning from main\n");

我(不幸的是)没有选择更改库的选项,但是我仍然希望JVM将控制权交还给C调用函数(以便我可以执行各种清理任务,等等.).有没有办法让JNI做到这一点,或者我是SOL?

I (unfortunately) don't have the option of changing the library, but I'd still like for the JVM to return control back to the C calling function (so I can do various cleanup tasks, etc..). Is there a way to get JNI to do that, or am I SOL?

谢谢

推荐答案

对于这种简单的情况,您不需要字节码编辑,在旧的Java中实现了很多安全处理.

You do not need bytecode editing for so simple case, a lot of security handling is implemented in the good old java.

System.setSecurityManager(SecurityManager)checkExit()中引发一些错误(例如ThreadDeath),并假定在同一线程中调用System.exit(int) [erm Runtime.getRuntime().exit(int)],它应该执行此操作.

System.setSecurityManager(SecurityManager) throw some Error (like ThreadDeath) in checkExit() and assuming System.exit(int) [erm Runtime.getRuntime().exit(int)] is invoked in the same thread, it should do it.

这篇关于如何在从JNI调用的代码中捕获System.exit()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆