我在Windows 7上运行jdk。我尝试在我的java应用程序中运行外部软件(pocketsphinx_continous.exe)。该软件永久运行(pocketsphinx_continous.exe)并将一些输出打印到我想通过我的java应用程序阅读的控制台。
如果我运行" pocketsphinx_continous.exe"一些来自命令行的参数都运行良好,我看到了软件的输出。在杀死进程后,我尝试在我的java应用程序中运行它。但是java打印没有输出到控制台。
这是我的代码:
public void start(){
try {
Runtime rt = Runtime.getRuntime();
String[] commands = {"D:/cmusphinx/pocketsphinx/bin/Release/x64/pocketsphinx_continuous.exe", "-hmm", "d:/cmusphinx/pocketsphinx/model/en-us/en-us", "-lm", "d:/cmusphinx/pocketsphinx/model/en-us/en-us.lm.bin", "-dict", "d:/cmusphinx/pocketsphinx/model/en-us/cmudict-en-us.dict", "-samprate", "16000/8000/48000", "-inmic", "yes"};
Process proc = rt.exec(commands);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
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;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Java只会打印"这是命令的标准输出:"仅此而已。但它不会崩溃,它仍然运行没有任何错误。在我看来,java将等到执行的命令完成,直到它打印任何东西。但该软件将永久运行并打印一些新结果......
有什么想法吗?
祝你好运
麦克
答案 0 :(得分:1)
我建议你做以下事情:
Process p = null;
ProcessBuilder b = new ProcessBuilder("D:/cmusphinx/pocketsphinx/bin/Release/x64/pocketsphinx_continuous.exe -hmm d:/cmusphinx/pocketsphinx/model/en-us/en-us -lm d:/cmusphinx/pocketsphinx/model/en-us/en-us.lm.bin -dict d:/cmusphinx/pocketsphinx/model/en-us/cmudict-en-us.dict -samprate 16000/8000/48000 -inmic yes");
try {
p = b.start();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader output = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
try {
while ( (line = output.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
使用ProcessBuilder
您不必分开参数。只需在String
中复制整个命令。