Posts

Showing posts from January, 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
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4254231