我不知道为什么会挂起。我正在 try catch 通过 commons-exec 运行的进程的输出,但我继续挂起。我在下面提供了一个示例程序来演示此行为。
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?");
}
}
最佳答案
根据javadoc , execute(CommandLine command)
是同步的,execute(CommandLine command, ExecuteResultHandler handler)
是异步的。
关于java - commons-exec : hanging when I call executor. 执行(命令行);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2702834/