4

I am running a shell command from Java code using ProcessBuilder.start() I need a call-back (or some sort of notification) when the command finishes execution. The command takes 10-15 seconds to execute. Is it possible using ProcessBuilder?

2 Answers 2

5

The start() method of ProcessBuilder clearly states it returns a Process, whose API is here. That Process has methods that can be called on it, including waitFor, which will wake up the current thread when the Process has finished. All you need to do is start a thread, give it this process and have it signal when the Process finishes, or after a timeout.

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

Comments

5

Yes it's possible. You could call waitFor() on the Process, and wait for it to return, and then analyze the returned value to be sure that it's 0, or if not respond to the error. This is often done in a background thread so as not to tie up the current thread.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.