1

my java code runs a main method that throws exit code.

How can I catch that exit code and not stop the runtime?

        ConfigValidator.main(new String[] {"-dirs", SdkServiceConfig.s.PROPERTIES_FILE_PATH});

I thought to use

        ProcessBuilder builder = new ProcessBuilder(commands);
        Process process = builder.start();
        int waitFor = process.waitFor();
        int a = process.exitCode()

but how can i run this to run java code?

I cannot change the code inside the "main" method

Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • The information you provided seems to be incomplete. Did you consider wrapping the stuff in a try catch in main()? – Adder Jun 13 '17 at 14:46
  • found this post: https://stackoverflow.com/questions/5549720/how-to-prevent-calls-to-system-exit-from-terminating-the-jvm – Elad Benda Jun 13 '17 at 15:12

2 Answers2

2

The java code does not throw an exit code.

That's because an exit code is an integer, and you cannot throw an integer, you can only throw an exception.

The java code exits with an exit code.

(I wish I could say that the java code returns an exit code, but unfortunately, the designers of the java language decided to make the main() function return void instead of an exit code, which makes things a bit difficult.)

The following stackoverflow answer suggests to use a security manager that prevents invocation of System.exit(). So, an attempt to invoke System.exit() will result in a SecurityException, and then you can catch that exception from the point where you invoke the main() function.

Stackoverflow: Java: How to test methods that call System.exit()?

But of course the right way to do things is to restructure main() so that it does not invoke System.exit() so that you do not have to do hacky things like that.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • thanks for the link. but how can i use it from non-JUNIT code? – Elad Benda Jun 13 '17 at 15:06
  • That answer consists of two parts, one which I mentioned above, and another which is junit-specific. You can ignore the junit-specific second part. Also, there is nothing magical about junit: if you needed to use it in non-testing code, you can. And if you are doing such awfully hacky things as invoking `main()` from production code, then including junit should be the least of your problems. – Mike Nakis Jun 13 '17 at 15:09
  • I used java securityManager, but then i get access permissions denied whenever i try to open any file – Elad Benda Jun 14 '17 at 08:23
  • Sorry, I don't know what to say. I have no prior experience with deliberately enabling a security manager. If my answer is not helping you, do not "accept" it. Someone else might post an answer that might be more useful. – Mike Nakis Jun 14 '17 at 08:36
0

Please try to use Thread.sleep() method to avoid program exit but I not sure it is the proper solution for you. Probably you need to interrupt execution of your app by system inputs or smth. like this.

Andriy Br.
  • 89
  • 5