【发布时间】:2012-09-16 17:06:14
【问题描述】:
我正在 Windows 中运行一个从 Windows 事件中收集日志的 java 程序。将创建一个 .csv 文件,在该文件上执行某些操作。
命令被执行和管道化。如何让我的 Java 程序等待进程完成?
这是我正在使用的代码 sn-p:
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.
【问题讨论】:
-
或者你可以考虑使用 Commons-exec: commons.apache.org/exec/tutorial.html