commons-exec:当我调用 executor.execute(commandLine) 时挂起;

发布于 2024-08-29 17:20:21 字数 1006 浏览 6 评论 0原文

我不知道为什么这个挂了。我试图捕获通过 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?");
}
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新雨望断虹 2024-09-05 17:20:21

根据javadoc执行(CommandLine command) 是同步的,而 execute(CommandLine command, ExecuteResultHandler handler) 是异步的。

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

总攻大人 2024-09-05 17:20:21

您调用的命令 java 将输出生成到其标准输出流。该流必须由您的调用程序泵入输入流。这不会发生在你的程序中。

您必须在单独的线程中读取输入流(is 在您的代码中),因为这就是管道流的工作方式。请注意,您必须在调用 execute() 之前启动读取线程。

另请参阅从 Apache Commons-Exec 捕获大量输出

根据您的其他问题使用commons-exec流式输出?您期望大数据,因此您必须使用管道流,并且不能使用使用 ByteArrayInputStream 作为输出的更简单方法。您在那里给出的答案与您在这里的代码存在同样的问题。

The command you invoked, java, produces output to its standard output stream. That stream must be pumped into an input stream by your invoking program. This does not happen in your program.

You have to read the input stream (is in your code) in a separate thread, because that is how piped streams work. Note that you must start the reading thread before calling execute().

See also Capturing large amounts of output from Apache Commons-Exec

According to your other question Streaming output with commons-exec? you expect large data, so you must use the piped streams and cannot use the simpler approach of using a ByteArrayInputStream as output. The answer you give yourself there, suffers from the same problem as your code here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文