Alee、そうですね、Runtime.getRuntime()で実行できます。exec( "file.bat");その後、あなたは、たとえば、実行
の出力をキャプチャすることができ、2つのオプションがあります:
public static void main(String[] args) {
System.out.println("hello");
try {
Process p = Runtime.getRuntime().exec("./prog.sh");
InputStream in = p.getInputStream();
System.out.println("OUTPUT");
StringBuilder sb = new StringBuilder();
int c;
while((c = in.read()) > 0) {
sb.append((char) c);
}
//here the script finished
String output = sb.toString();
if(output.contains("Exception")) {
System.out.println("script failed");
}
if(p.exitValue() == 0) {
System.out.println("The script run without errors");
} else {
System.out.println("The script failed");
}
} catch (IOException e) {
e.printStackTrace();
}
}
これは、あなたが出力をキャプチャして、スクリプトが正常に実行するかどうかを決定する必要があり、両方のシナリオを、キャプチャまたはスクリプトからの終了ステータスを使用できるかどうかを確認します。
exitValueコードは、成功の場合は0、失敗の場合はその他の数値です。
この出力内の特定の文字列を検索できますか?そのコンソールで例外が発生し、適切な終了コードでコンソールが終了することがあります。プロセスは例外のために完了しませんでした。どうすればそれを処理できますか? –
はい、上記の更新を確認してください – Augusto
Windowsでは、完了するためにexec'dプロセスのstdout/stderrストリームを消費する必要があります。 – maerics