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

查看:28
本文介绍了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天全站免登陆