Java – commons exec: when I call the executor Suspend when execute (commandline);

I don't know why I try to capture the output of the process through commons exec, and then I continue to hang I have provided a sample program to demonstrate this behavior

import java.io.DataInputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.PumpStreamHandler;
public class test {

public static void main(String[] args) {
    String command = "java";

    PipedOutputStream output = new PipedOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(output);

    CommandLine cl = CommandLine.parse(command);

    DefaultExecutor exec = new DefaultExecutor();
    DataInputStream is = null;
    try {
        is = new DataInputStream(new PipedInputStream(output));
        exec.setStreamHandler(psh);
        exec.execute(cl);
    } catch (ExecuteException ex) {
    } catch (IOException ex) {
    }

    System.out.println("huh?");
}
}

Solution

According to Javadoc, execution (commandline command) is synchronous, and execution (commandline command, executeresulthandler handler) is asynchronous on the other hand

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>