我有一個Java程序可以在OS中執行特定命令。我也使用Process.waitfor()(如下面的代碼所示)來指示執行是否成功完成或失敗。在操作系統中執行命令的Java
我的問題是,有沒有其他的方式來避免使用process.waitfor(),有沒有辦法使用while循環,並執行某些操作,直到該過程完成?
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(cmdFull);
BufferedReader inStream = new BufferedReader(new InputStreamReader(p.getInputStream()));
String inStreamLine = null;
String inStreamLinebyLine=null;
while((inStreamLine = inStream.readLine()) == null) {
inStreamLinebyLine = inStreamLinebyLine+"\n"+inStreamLine;
}
try {
rc = p.waitFor();
} catch (InterruptedException intexc) {
System.out.println("Interrupted Exception on waitFor: " +
intexc.getMessage());
}
啥子我希望做的,是這樣的
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(cmdFull);
BufferedReader inStream = new BufferedReader(new InputStreamReader(p.getInputStream()));
String inStreamLine = null;
String inStreamLinebyLine=null;
while((inStreamLine = inStream.readLine()) == null) {
inStreamLinebyLine = inStreamLinebyLine+"\n"+inStreamLine;
}
try {
while ((rc = p.waitFor()) == true) { // This is made up, I don't even think it would work
System.out.println('Process is going on...');
}
} catch (InterruptedException intexc) {
System.out.println("Interrupted Exception on waitFor: " +
intexc.getMessage());
}
感謝,