0

I am using below command to execute a shell script from java. I also need to check if script execution is successful without any errors. Is there any possible method to do this? Please advise.

ProcessBuilder pb = new ProcessBuilder("test.sh", arg1, arg2);
Process p = pb.start();
1
  • Thank you :) somehow it did not come up in the suggestions while typing question. Let me try that Commented May 3, 2016 at 15:33

2 Answers 2

1

You need to check if the process terminated which you can do with Process.waitFor() which blocks until the process completed. The return value of this call is the return code of the system command you invoked.

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

2 Comments

Thank you. can i use p.waitfor() followed by getting p.exitvalue() and checking if it is zero?
p.waitFor() returns the exit code, you can check that for zero: p.waitFor() == 0 -> success
1

To handle asynchronously, attach a threaded listener to Process.getErrorStream (ie listen to the read() method). When the process closes, you will get an IOException, releasing the thread. Then call Process.exitValue(). If you got anything coming off of the error stream or Process.exitValue() is not == 0, then that hints to probable failure

1 Comment

Thank you..will try this...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.