我正在Windows中運行一個從Windows事件中收集日誌的java程序。將創建一個.csv文件,用於執行某些操作。Java:等待exec進程直到它退出
命令被執行並傳送。我怎樣才能讓我的Java程序等待進程完成?
這裏是我使用的代碼片段:
Runtime commandPrompt = Runtime.getRuntime();
try {
Process powershell = commandPrompt.exec("powershell -Command \"get-winevent -FilterHashTable @{ logname = 'Microsoft-Windows-PrintService/Operational';StartTime = '"+givenDate+" 12:00:01 AM'; EndTime = '"+beforeDay+" 23:59:59 '; ID = 307 ;} | ConvertTo-csv| Out-file "+ file +"\"");
//I have tried waitFor() here but that does not seem to work, required command is executed but is still blocked
} catch (IOException e) { }
// Remaining code should get executed only after above is completed.
另外,您可以考慮使用共享-EXEC:http://commons.apache.org/exec/tutorial .html –