簡體   English   中英

等待過程完成,然后再使用Java進行操作

[英]Wait for process to finish before proceeding in Java

本質上,我正在制作一個小型程序,該程序將安裝一些軟件,然后再運行一些基本命令來准備該程序。 但是,正在發生的情況是該程序開始安裝,然后立即移至以下幾行(注冊,更新等)。 當然,只有在完全安裝后才能實現,因此,我想找到一種方法來等待第一個進程,然后再運行第二個進程。 例如,

    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");

調用Process#waitFor() 它的Javadoc說:

如有必要,使當前線程等待,直到此Process對象表示的Process終止。

獎勵:您將獲得子流程的退出值。 因此,您可以檢查它是否以代碼0成功退出,或者是否發生錯誤(退出代碼為非零)。

您可以使用Process.waitFor()方法

而醫生說

如有必要,使當前線程等待 ,直到此Process對象表示的進程終止。 如果子進程已經終止,則此方法立即返回。 如果子進程尚未終止,則調用線程將被阻塞,直到子進程退出。

包括waitFor() 就您而言,您的代碼將如下所示。

    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");

如果您正在運行返回非常長的響應字符串的系統命令,則stdin緩沖區將填滿,並且進程似乎掛起。 sqlldr發生在我身上。 如果真是這樣,請在進程運行時從stdin中讀取。

    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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM