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

2
  • The information you provided seems to be incomplete. Did you consider wrapping the stuff in a try catch in main()? Commented Jun 13, 2017 at 14:46
  • found this post: stackoverflow.com/questions/5549720/… Commented Jun 13, 2017 at 15:12

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the link. but how can i use it from non-JUNIT code?
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.
I used java securityManager, but then i get access permissions denied whenever i try to open any file
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.