我想在我的Java程序中使用Linux命令行工具。我使用Process类(http://download.oracle.com/javase/6/docs/api/java/lang/Process.html)启动程序并获取输出:
/* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Process proc = Runtime.getRuntime().exec("octave");
BufferedReader reader =
new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader errorReader =
new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedWriter writer =
new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
int c;
while((c = proc.getInputStream().read()) != -1) {
System.out.print((char)c);
}
System.out.println("End");
}
我得到了以下输出:
GNU Octave,版本3.0.5版权 (C)2008年John W. Eaton和其他人。 这是免费软件;有关复制条件的源代码,请参见。 绝对没有保修;甚至不适用于特定目的。有关详细信息,请键入“warranty”。
Octave已配置为“i486-pc-linux-gnu”。
有关Octave的其他信息可在http://www.octave.org上找到。
如果您发现此软件有用,请做出贡献。有关更多信息,请访问http://www.octave.org/help-wanted.html 报告错误给(但请先阅读http://www.octave.org/bugs.html以了解如何编写有用的报告)。
有关从以前版本进行更改的信息,请键入“news”。
奇怪的是,如果我在终端中运行octave,则正常输出如下所示:
:〜/ workspace / Console / src / c $ octave GNU Octave,版本3.0.5 版权所有(C)2008年John W. Eaton和其他人。 这是免费软件;有关复制条件的源代码,请参见。 绝对没有保修;甚至不适用于特定目的。有关详细信息,请键入“warranty”。
Octave已配置为“i486-pc-linux-gnu”。
有关Octave的其他信息可在http://www.octave.org上找到。
如果您发现此软件有用,请做出贡献。 有关更多信息,请访问http://www.octave.org/help-wanted.html 报告错误给(但请先阅读http://www.octave.org/bugs.html以了解如何编写有用的报告)。
有关从以前版本进行更改的信息,请键入“news”。
octave:1>
因此,在请求输入的行中发送的字符未发送到我的输入流中。为什么?无法检测到是否需要输入吗?
感谢您的回答!
Heinrich