在继续Java之前等待进程完成 [英] Wait for process to finish before proceeding in Java

查看:112
本文介绍了在继续Java之前等待进程完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在制作一个小程序,它将安装一些软件,然后运行一些基本命令来准备该程序。但是,正在发生的是程序开始安装,然后立即转到以下行(注册,更新等)。当然,在完全安装之前不会发生这种情况,因此我想在运行第二个进程之前找到一种等待第一个进程的方法。例如,

Essentially, I'm making a small program that's going to install some software, and then run some basic commands afterwards to prep that program. However, what is happening is that the program starts its install, and then immediately moves on to the following lines (registration, updates, etc). Of course, that can't happen until it's fully installed, so I'd like to find a way of waiting on the first process before running the second. For example,

    Main.say("Installing...");
    Process p1 = Runtime.getRuntime().exec(dir + "setup.exe /SILENT");
    //Wait here, I need to finish installing first!
    Main.say("Registering...");
    Process p2 = Runtime.getRuntime().exec(installDir + "program.exe /register aaaa-bbbb-cccc");
    Main.say("Updating...");
    Process p4 = Runtime.getRuntime().exec(installDir + "program.exe /update -silent");


推荐答案

致电 处理#WAITFOR() 。它的Javadoc说:

Call Process#waitFor(). Its Javadoc says:


如果需要,导致当前线程等待,直到此 Process <所代表的进程为止/ code>对象已终止。

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated.

奖励:您获得子流程的退出值。因此,您可以使用代码 0 检查是否已成功退出,或者是否发生错误(非零退出代码)。

Bonus: you get the exit value of the subprocess. So you can check whether it exited successfully with code 0, or whether an error occured (non-zero exit code).

这篇关于在继续Java之前等待进程完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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