9

Given the class below,

public class ClassOne {
    public static void main(String... args) {
        System.exit(1);
    }
}

The following class will be destroyed as well, assuming there are other things to do after ClassOne.main is invoked.

public class ClassTwo {
    public static void main(String... args) {
        ClassOne.main(args);
        Thread.sleep(30000);
    }
}

Is there a way to ignore the System.exit(1); of ClassOne in ClassTwo's invocation?

setzamora
  • 3,560
  • 6
  • 34
  • 48
  • possible duplicate of [Java: How to test methods that call System.exit()?](http://stackoverflow.com/questions/309396/java-how-to-test-methods-that-call-system-exit) – John Kugelman Jun 18 '13 at 18:04

1 Answers1

14

You can't ignore it per se, but you can prevent it from terminating the JVM via SecurityManager. Take a look at this question for detailed code example.

Community
  • 1
  • 1
ChssPly76
  • 99,456
  • 24
  • 206
  • 195
  • Sorry but what is _se_ ("You can't ignore it per _se_")? – Matt Warrick Jan 15 '12 at 21:26
  • "per se" is Latin meaning "as such". In other words, you can't ignore the `System.exit()` as if it never happened, but you can prevent it from terminating the JVM, and the `exit()` call will instead throw a Security Exception. However, I wonder if this exception could kill the system in some cases... – DNA Sep 23 '12 at 19:37
  • So what if the other class then re-enables it? – TheRealChx101 Sep 18 '16 at 00:09