commons-exec:我调用executor.execute(commandLine)时挂起; [英] commons-exec: hanging when I call executor.execute(commandLine);

查看:745
本文介绍了commons-exec:我调用executor.execute(commandLine)时挂起;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么这挂了.我正在尝试捕获来自通过commons-exec运行的进程的输出,并且我继续挂起.我在下面提供了一个示例程序来演示此行为.

I have no idea why this is hanging. I'm trying to capture output from a process run through commons-exec, and I continue to hang. I've provided an example program to demonstrate this behavior below.

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?");
}
}

推荐答案

根据

According to the javadoc, execute(CommandLine command) is synchronous, execute(CommandLine command, ExecuteResultHandler handler) on the other hand is asynchronous.

这篇关于commons-exec:我调用executor.execute(commandLine)时挂起;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆