Handling the IllegalThreadStateException
Invoke Process#waitFor()
before trying to get the exit value. This blocks the current thread until the spawned process terminates. If you don't do this, Process#exitValue()
throws
IllegalThreadStateException - if the subprocess represented by this
Process
object has not yet terminated
Always use waitFor(long timeout,TimeUnit unit) instead of waitFor(), so that the thread will not be blocked indefinitely.