Java:等待exec进程直到它退出 [英] Java: wait for exec process till it exits

查看:952
本文介绍了Java:等待exec进程直到它退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows中运行一个java程序,用于从Windows事件中收集日志。创建一个.csv文件,在该文件上执行某些操作。

I am running a java program in Windows that collects log from Windows events. A .csv file is created on which certain operations are to be performed.

执行命令和管道。如何让我的Java程序等到进程完成?

The commands are execed and piped. How can I cause my Java program to wait until the process is finished?

这是我正在使用的代码片段:

Here is the code snippet I am using:

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.


推荐答案

你需要使用 waitFor ()而不是 wait()。这样你的线程就会阻塞,直到执行完的命令结束。

You need to use waitFor() instead of wait(). That way your thread will block until the executed command finishes.

这篇关于Java:等待exec进程直到它退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆