0

でバイナリファイルを実行したときにエラーメッセージを取得する方法はRuntime?

私のコードは次のとおりです。

Runtime localRuntime = Runtime.getRuntime();
String strExec = "myBinary -s -a myconfigbinary.conf";
try {
    localRuntime.exec(strExec);
    System.out.println("Success execute."); 
} catch (IOException e) {
    System.out.println(e.getMessage().toString());      
}

私のコードは上記のとおりです。ファイルが存在しない場合にのみ例外エラーが発生します。しかし、構成ファイルのエラーまたはバイナリが実行されていない原因が原因で機能しなかったときに、コンピューターでコンソールを使用して実行すると、まだメッセージが表示されますSuccess execute.

私の質問は、コンピューターのコンソールでエラーが発生したときのようなエラー メッセージを取得したいということです。この場合、正しい例外を使用するにはどうすればよいですか?

ありがとう。

4

2 に答える 2

1

ErrorStreamのプロセスのjavaもキャプチャする必要があります。

try {
   Process proc = localRuntime.exec(strExec);
   InputStream stderr = proc.getErrorStream();
   InputStreamReader is = new InputStreamReader(stderr);
   BufferedReader br = new BufferedReader(is);
   String line = null;
   while ( (line = br.readLine()) != null)
          System.out.println(line);
   int exitVal = proc.waitFor();
   System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t) {
    t.printStackTrace();
}

exec されたプロセスからのエラーがstdout.

于 2013-01-12T18:39:00.580 に答える
1

次のようなエラーストリームを確認できます

Process process=localRuntime.exec(strExec);
process.waitFor();
InputStream errorStream = process.getErrorStream();
于 2013-01-12T18:38:11.467 に答える