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

标签 java multithreading apache-commons-exec

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

相关文章:

java - JSR-133 说明书如何强制执行 Java 内存模型做出的所有保证

c++ - 生产者-消费者 : Lost Wake-up issue

java - Android - 以编程方式对齐 TableRow 列

java - 在 Mac OS X El Capitan 上通过 Java 使用 AppleScript

java - 带有 SimpleCursorAdapter 的 Android ListView - 恢复时崩溃

java - 如何使用 Selenium 框架从受用户名和密码保护的站点下载文件?

java - PumpStreamHandler可以实时捕获流程输出

java无法运行命令行应用程序

java - @Async 与 Spring 3.0.6

java - Apache CommonsExec 异步调用阻止同步调用