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

Tomcat - removing server info

Below are the steps to remove Tomcat Server Information 1. Make sure Tomcat is not running 2. Navigate to "Tomcat Installation"...