首先,我想说我读了关于apache commons exec的所有其他主题。我有一个基于终端的可执行文件运行时,它会提示用户从命令行输入一些内容。我需要从我的java程序中运行这个程序并将一些命令提供给程序。我使用apache commons exec。这就是我到目前为止所做的:
CommandLine command = new CommandLine("myExec");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer();
TextareaOutputStream outputstream = new TextareaOutputStream(jTextArea1); //this redirects the output to a text area by extending and overriding outputstream. Works perfect.
PumpStreamHandler streamHandler = new PumpStreamHandler(outputstream, outputstream);
executor.setStreamHandler(streamHandler);
OutputStream out = new ByteArrayOutputStream();
executor.getStreamHandler().setProcessInputStream(out);
executor.setProcessDestroyer(processDestroyer);
executor.execute(command, resultHandler);
out.write("myFile.dat\n".getBytes());
这不起作用。一旦我的程序启动就崩溃了:
forrtl: severe (24): end-of-file during read, unit 5, file stdin
我也尝试过使用pipedinputstream - > Trouble providing multiple input to a Command using Apache Commons Exec and extracting output:
CommandLine command = new CommandLine("myExec");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer();
TextareaOutputStream outputstream = new TextareaOutputStream(jTextArea1); //this redirects the output to a text area by extending and overriding outputstream. Works perfect.
PipedInputStream stdin = new PipedInputStream();
BufferedOutputStream processInput = new BufferedOutputStream(new PipedOutputStream(stdin));
PumpStreamHandler streamHandler = new PumpStreamHandler(outputstream, outputstream, stdin);
executor.setStreamHandler(streamHandler);
executor.setProcessDestroyer(processDestroyer);
executor.execute(command, resultHandler);
processInput.write("myFile.dat\n".getBytes());
processInput.flush();
有了这个,它会挂起第一个提示!好像flush()没有效果。终端程序永远不会得到任何东西。
我也尝试过这个想法:How to pipe a string argument to an executable launched with Apache Commons Exec?
如果我在启动exec之前在ByteArrayInputStream中写入所有内容,那么它就可以了。如果我启动exec然后尝试写一些东西,那么我再次得到第一个错误。
真的很感激任何帮助。
为了完整性(我没有写这个,我不记得我从互联网上得到它)。这是为了将输出重定向到jTextArea:
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
class TextareaOutputStream extends OutputStream {
private final JTextArea area;
private final StringBuffer buf = new StringBuffer(128);
public TextareaOutputStream(final JTextArea area) throws IOException {
this.area = area;
}
@Override
public void write(int c) throws IOException {
// append character to buffer
buf.append((char) c);
// and newline appends to textarea
if (c == '\n' || c == ':') {
flush();
}
}
@Override
public void close() {
flush();
}
@Override
public void flush() {
SwingUtilities.invokeLater(new Runnable() {
String str = buf.toString();
@Override
public void run() {
area.append(str);
}
});
buf.setLength(0);
}
public void message(String msg) {
if (buf.charAt(buf.length() - 1) != '\n') {
buf.append('\n');
}
buf.append(msg);
buf.append('\n');
flush();
}
}