Stopping System.exit

|

Java has a robust and flexible security framework to control many aspects of a running application. Blocking System.exit() is one of those fined grain things that can be easily controlled with a SecurityManager. But why? If you don't have control over the entire code base (e.g. a third party library) then to make sure your app will keep on running it may just be something you have to do.

    final SecurityManager securityManager = new SecurityManager() 
        {
            public void checkPermission(Permission permission) 
            {
                if ("exitVM".equals(permission.getName())) 
                {
                    throw new SecurityException("System.exit attempted and blocked.");
                }
            }
        };
    System.setSecurityManager(securityManager);