0

I am trying to run an interactive executable from Java application using ProcessBuilder; it's supposed to take input, produce output and then wait for the next input. The main problem here with Input/Output streams. I send an input and get nothing. Here is the code:

private static Process process;
private static BufferedReader result;
private static PrintWriter input;

process = new ProcessBuilder("compile-lm", lmFile.toString(), " --score yes").redirectErrorStream(true).start();

input = new PrintWriter(new OutputStreamWriter(process.getOutputStream()), true);
input.println(message);
System.out.println(message);

result = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = new String();

while ((line = result.readLine()) != null)
{

/* Some processing for the read line */

System.out.println("output:\t" + line);
}
4

3 回答 3

1

我已经尝试过您的代码,它工作正常,代码没有问题。我认为您尝试执行的命令存在问题(它什么都不返回)。尝试更改参数甚至更改整个命令进行测试。如果您可以在其他地方执行命令(例如终端尝试它并查看具有相同参数的输出)

于 2012-08-01T17:02:50.463 回答
0

我已经多次使用过类似的设置,但现在找不到工作副本:(我的第一反应是将初始化阅读器(结果变量)的行移到将命令发送到的行之前进程(input.println(消息))。

于 2012-08-01T09:42:09.367 回答
0

尝试关闭进程的输出流。基本上,您受子进程输出端发生的任何缓冲的支配。

于 2012-08-01T11:01:02.497 回答