【发布时间】:2014-07-12 14:25:37
【问题描述】:
我有一个计算机代数程序(称为 Reduce),它以交互方式在 shell 中工作:在 shell 中启动 Reduce,然后您可以定义变量、计算这个和那个,以及什么不。 Reduce 将输出打印到 shell 中。我的想法是,我想为这个基于文本的程序构建一个前端,评估其输出并将其转换为一个不错的 LaTeX 样式公式。为此,我想使用 Java。
我可以通过 exec() 启动 Reduce。但是我怎样才能模拟文本输入到打开的 shell,我怎样才能读回 Reduce 写入 shell 的内容?
谢谢 延斯
编辑 1:当前代码
// get the shell
Runtime rt = Runtime.getRuntime();
// execute reduce
String[] commands = {"D:/Programme/Reduce/reduce.com", "", ""};
Process proc = null;
try {
proc = rt.exec(commands);
} catch (Exception e) {
System.out.println("Error!\n");
}
// get the associated input / output / error streams
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedWriter stdOutput = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
try {
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
try {
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
}
【问题讨论】:
标签: java