开发者

How to prevent calls to System.exit() from terminating the JVM?

I am almost certain this is impossible, but it's worth a try.

I am writing a command line interface for a certain tool. I am talking about a Java application th开发者_开发问答at invokes another Java application. The tool calls System.exit after execution, which in turn terminates my own execution environment. I don't want that.

Is there any way to ignore System.exit calls?


Yes, this is possible using a SecurityManager. Try the following

class MySecurityManager extends SecurityManager {
  @Override public void checkExit(int status) {
    throw new SecurityException();
  }

  @Override public void checkPermission(Permission perm) {
      // Allow other activities by default
  }
}

In your class use the following calls:

myMethod() {
    //Before running the external Command
    MySecurityManager secManager = new MySecurityManager();
    System.setSecurityManager(secManager);

    try {
       invokeExternal();
    } catch (SecurityException e) {
       //Do something if the external code used System.exit()
    }
}


You can break your application in two parts. The first one gets started by the tool. Then you launch the second part of your application as a new process. Then the host application kills your first part, but the second part is still running.

This way the first part of your app is just the startup for the second part which is in turn your real application.


Set the SecurityManager to ignore System.exit(), unless it comes from your code.


Regarding to this (@Vonc answer) you should use Security Manager:

Try modifying the TestCase to run with a security manager that prevents calling System.exit, then catch the SecurityException.


Java 17 update:

A lot of the earlier answers to this question mention the use of a custom SecurityManager. This was correct at the time. But as of Java 17, System.setSecurityManager() is deprecated for removal, so this approach won't work in the future.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜