gpt4 book ai didi

java - commons-exec : hanging when I call executor. 执行(命令行);

转载 作者:搜寻专家 更新时间:2023-11-01 04:07:04 24 4
gpt4 key购买 nike

我不知道为什么会挂起。我正在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com