Wait for process to finish before proceeding in Java

后端 未结 4 1277
無奈伤痛
無奈伤痛 2020-12-01 18:24

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

相关标签:
4条回答
  • 2020-12-01 19:00

    Call Process#waitFor(). Its Javadoc says:

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

    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).

    0 讨论(0)
  • 2020-12-01 19:16

    If you are running a system command that returns a very long response string, stdin buffer fills up and the process appears to hang. This happened to me with sqlldr. If that appears to be the case then just read from stdin as the process is running.

        try {
            ProcessBuilder pb = new ProcessBuilder("myCommand");
            Process p = pb.start();
            BufferedReader stdInput = new BufferedReader(new 
                    InputStreamReader(p.getInputStream()));
    
            BufferedReader stdError = new BufferedReader(new 
                    InputStreamReader(p.getErrorStream()));
            StringBuffer response = new StringBuffer();
            StringBuffer errorStr = new StringBuffer();
            boolean alreadyWaited = false;
            while (p.isAlive()) {
                try {
                    if(alreadyWaited) {
    
                        // read the output from the command because
                        //if we don't then the buffers fill up and
                        //the command stops and doesn't return
                        String temp;
    
                        while ((temp = stdInput.readLine()) != null) {
                            response.append(temp);
                        }
    
    
                        String errTemp;
                        while ((errTemp = stdError.readLine()) != null) {
                            errorStr.append(errTemp);
                        }                                                   
                    }
                    Thread.sleep(1000);
                    alreadyWaited = true;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                logger.debug("Response is " + response);
                logger.debug("Error is: " + errorStr);
            }
    
        } catch (IOException e) {
            logger.error("Error running system command", e);
        }
    
    0 讨论(0)
  • 2020-12-01 19:19

    Include waitFor(). In your case, your code will look something like below.

        Main.say("Installing...");
        Process p1 = Runtime.getRuntime().exec(dir + "setup.exe /SILENT");
        p1.waitFor()
        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");
    
    0 讨论(0)
  • 2020-12-01 19:25

    you can use Process.waitFor() method

    and the doc says

    Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.

    0 讨论(0)
提交回复
热议问题