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

No comments:

Post a Comment

Hadoop - How to setup a Hadoop Cluster

Below is a step-by-step guide which I had used to setup a Hadoop Cluster Scenario 3 VMs involved: 1) NameNode, ResourceManager - Host...