Thursday, January 7, 2010

Java - Calling External Process

Calling external process for Java is simple. Runtime.getRuntime().exec(command) will create a sub-process to perform the given command.

But sometime, we needs to wait for the sub-process to complete before continuing the Java application. In Java-Doc, Process.waitfor() seems to be a fine options, however, many people encounter infinite hang when the waitfor method is called.

Till now, I do not have fruitful success on waitfor. The most common workaround is to do the following step

1. Create a process Process p = Runtime.getRuntime().exec(cmd);
2. Get the input stream InputStream in = p.getInputStream();
3. Get the error stream InputStream err = p.getErrorStream();
4. Create a loop to read out all input stream, error stream and check on Process.exitValue();

Just do a search in Google will give you some workaround. For example

Windows 7 - How to reset Windows 7 password

This is just a post of what I had found in the Internet and it works for me (See reference for the actual post). While I knew that there a...