从Java运行Unix命令非常简单.
Runtime.getRuntime().exec(myCommand);
Run Code Online (Sandbox Code Playgroud)
但是可以从Java代码运行Unix shell脚本吗?如果是,从Java代码中运行shell脚本是一个好习惯吗?
我在Windows中运行一个java程序,用于从Windows事件中收集日志.创建.csv文件,在该文件上执行某些操作.
命令被执行和管道.如何让我的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.
Run Code Online (Sandbox Code Playgroud)