在开发运行其他开发人员编写的代码的容器时,请谨慎防范System.exit调用。 如果开发人员无意间调用了System.exit并将其代码部署为由您的容器运行,则它将完全降低容器进程。 可以使用SecurityManager中的checkExit函数调用来控制。

根据SecurityManager checkExit的参考 :

当前的安全管理器通过Runtime类的exit方法调用此方法。 状态0表示成功;状态0表示成功。 其他值表示各种错误。

因此,任何对exit的调用都将调用此方法,并且如果我们不希望继续进行处理,则只需抛出异常。 我们将SecurityManager定义如下:

public class StopExitSecurityManager extends SecurityManager{private SecurityManager _prevMgr = System.getSecurityManager();public void checkPermission(Permission perm){}public void checkExit(int status){super.checkExit(status);throw new ExitTrappedException(); //This throws an exception if an exit is called.}public SecurityManager getPreviousMgr() { return _prevMgr; }}

现在,我们可以提供一个易于使用的CodeControl类,如下所示:

public class CodeControl
{public CodeControl(){super();}public void disableSystemExit(){SecurityManager securityManager = new StopExitSecurityManager();System.setSecurityManager(securityManager) ;}    public void enableSystemExit(){SecurityManager mgr = System.getSecurityManager();if ((mgr != null) && (mgr instanceof StopExitSecurityManager)){StopExitSecurityManager smgr = (StopExitSecurityManager)mgr;System.setSecurityManager(smgr.getPreviousMgr());}elseSystem.setSecurityManager(null);}
}

现在可以按以下方式使用CodeControl:

CodeControl control = new CodeControl();
try
{control.disableSystemExit();//invoke the methods and other classes that are not allowed to call System.exit.Object ret = invokeExecute(_method, runWith, parms);
}
finally
{//finally enable exitcontrol.enableSystemExit();
}

这样可以防止在disable中调用方法,并允许调用System.exit,但允许您的代码毫无问题地调用它。

参考: 防止我们的JCG合作伙伴 Raji Sankar 致电System.exit,该邮件来自Reflections博客。

翻译自: https://www.javacodegeeks.com/2013/11/preventing-system-exit-calls.html

防止System.exit调用相关推荐

  1. 安卓开发-Activity中finish() onDestroy() 和System.exit()的区别

    Activity.finish() Call this when your activity is done and should be closed.  在你的activity动作完成的时候,或者A ...

  2. Java: System.exit() 与安全策略

    说明 System.exit() 的本质是通知 JVM 关闭. 一般来说,有两种禁用 System.exit() 的办法: 安全管理器 安全策略 本质都是JRE 提供的本地实现,在执行之前进行权限判断 ...

  3. Android中finish()、System.exit()、KillProcess()的区别

    1. finish是Activity的类,仅仅针对Activity,当调用finish()时,只是将活动推向后台,并没有立即释放内存,活动的资源并没有被清理 2. 当调用System.exit(0)时 ...

  4. android finish 区别,Android Activity类finish、onDestory和System.exit介绍

    image.png finish函数 Activity.finish() Call this when your activity is done and should be closed. 在你的a ...

  5. bat 命令行执行 java(jar)获取返回值 【ERRORLEVEL、System.exit(code)】

    ■前言 bat调用java,如何判断java运行正常终了 ■实现 1.命令行中, 使用 %ERRORLEVEL% ↓ERRORLEVEL的介绍 https://blog.csdn.net/sxzlc/ ...

  6. [转载] Java——System.exit()用法及个人理解

    参考链接: Java中的System.exit() 该方法的原型是:System.exit(int status). 取值及作用 status由使用者自取,一般可取0,1或者其他数:当其为System ...

  7. Java中的System.exit()

    The System.exit() method terminates the Java virtual machine. This method takes an argument, which i ...

  8. Java笔记(6)-Math、BIgInteger、DecimalFormat、Pattern和Macth、Scanner、System.exit()

    我是天空里的一片云, 偶尔投影在你的波心-- 你不必讶异, 更无须欢喜-- 在转瞬间消灭了踪影. 你我相逢在黑夜的海上, 你有你的,我有我的,方向: 你记得也好, 最好你忘掉, 在这交会时互放的光亮. ...

  9. System.exit(0)和System.exit(1)区别

    2019独角兽企业重金招聘Python工程师标准>>> System.exit(0)是将你的整个虚拟机里的内容都停掉了 ,而dispose()只是关闭这个窗口,但是并没有停止整个ap ...

最新文章

  1. ping命令工具:同时ping多个IP
  2. 戈登贝尔奖获得者张林峰:当AI遇上物理模型,会有怎样的质变? | 智源专访...
  3. 吴恩达深度学习课程deeplearning.ai课程作业:Class 4 Week 1 Convolutional Neural Networks: Step by Step
  4. 互逆矩阵特征值,奇异值的关系
  5. CSS常用的选择器和优先级的权重问题
  6. RecyclerView实现滑动删除和拖拽功能
  7. linux如何打开url,用于打开URL的命令?
  8. yii2 错误处理
  9. 小芋头君的知乎 Live 直播-前端开发者成长之路
  10. NYOJ--114--某种序列(大数)
  11. 三菱GXWorks2 绘制梯形图
  12. 2015年春节往事小记
  13. 右手定则判断向量积的方向
  14. 【金融财经】金融市场一周简报(2017-09-22)
  15. matlab 面积函数,编写函数计算一个正方形的面积,并且在Matlab主函数中调用该函数。要求函数名为area,返回参数是...
  16. 第一届『Citric杯』NOIP提高组模拟赛 题解
  17. 微星GE62 NVIDIA960m 双系统ubuntu16.04 配置caffe-ssd
  18. qpOASES使用笔记
  19. 渗透测试-C段主机信息收集
  20. tcl计算机语言,Vivado之TCL脚本语言基本语法介绍

热门文章

  1. Eclipse导入他人的Maven工程报错
  2. java常用代码_Java 中常用代码 (欢迎补充)
  3. python弹出窗口 闪烁_Python。得到闪烁/闪烁的窗口
  4. 车提示检测轮胎气压_水淹车估价中心_辽宁中车检
  5. df、du和fdisk命令的区别
  6. jpa embedded_JPA @Embeddable和@Embedded
  7. vm 安装jdk1.8_JDK 13:VM.events已添加到jcmd
  8. java cuba_CUBA平台–新的Java企业应用程序框架
  9. 响应式多级菜单 侧边菜单栏_使用纯HTML和OmniFaces构建动态响应的多级菜单
  10. junit:junit_简而言之,JUnit:Hello World