私はWindows 7でjdkを実行しています。私はJavaアプリケーション内で外部ソフトウェア(pocketsphinx_continous.exe)を実行しようとしています。ソフトウェアは永久に実行され(pocketsphinx_continous.exe)、コンソールに出力を出力します。この出力は私のJavaアプリケーションで読むのが好きです。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が実行されたコマンドが何かを印刷するまで終了するのを待つようです。しかし、ソフトウェアは永久に実行され、いくつかの新しい結果を印刷します...
アイデア?
敬具
マイク
このメソッドを使用してみてください 'public int read(char [] cbuf、int off、int len)' readLine'の代わりにIOException'ここで 'while((s = stdInput.readLine())!= null){'あなたの出力が '\ n'で決して終わらないかもしれないからです。 – ACV
このプログラムがCやC++やその他の言語で書かれているのであれば、デフォルトで端末への出力は行単位で行われますが、ファイルやパイプへの出力はバッファリングされます。子プロセスで'yourcommandline |を試してください。コマンドプロンプトでfindstr。そこに出力が表示されない場合は、Javaでも出力されません。 –