2016-03-30 25 views
0

私は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が実行されたコマンドが何かを印刷するまで終了するのを待つようです。しかし、ソフトウェアは永久に実行され、いくつかの新しい結果を印刷します...

アイデア?

敬具

マイク

+0

このメソッドを使用してみてください 'public int read(char [] cbuf、int off、int len)' readLine'の代わりにIOException'ここで 'while((s = stdInput.readLine())!= null){'あなたの出力が '\ n'で決して終わらないかもしれないからです。 – ACV

+1

このプログラムがCやC++やその他の言語で書かれているのであれば、デフォルトで端末への出力は行単位で行われますが、ファイルやパイプへの出力はバッファリングされます。子プロセスで'yourcommandline |を試してください。コマンドプロンプトでfindstr。そこに出力が表示されない場合は、Javaでも出力されません。 –

答えて

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にコピーしてください。

+0

'Runtime.exec'には行全体のオーバーロードがあり、環境と作業ディレクトリのオーバーロードがあります。あなたが 'ProcessBuilder'で行うことができるのは' Runtime'でなく、stdin/out/errを継承またはリダイレクトすることだけです。 –

+0

これはうまくいきませんが、彼は "while(line = output.readLine())!= null){"エラーも出力もありません! – Mike

+0

外部アプリケーションが行単位で出力しないことがあります。 @ACVのように、 'read(char [] cbuf、int off、int len)'を試してください。 –

関連する問題