commons-exec:当我调用 executor.execute(commandLine) 时挂起;
我不知道为什么这个挂了。我试图捕获通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据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.您调用的命令
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 callingexecute()
.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.