【问题标题】:Process.waitFor() not "waiting" for current thread to finishProcess.waitFor() 不“等待”当前线程完成
【发布时间】:2020-10-07 08:27:29
【问题描述】:

我有一个 Excel 表,其中包含要执行的 jar 文件列表。为了开始测试,我将运行一个代码从上述 excel 表中读取并执行 excel 表中的 jar 文件。运行这些 jar 文件的代码如下:

final Process p = Runtime.getRuntime().exec("cmd /c start cmd /k java -jar " + start.jar_filepath + " " + start.tc_name + " " + start.test_data + " " + start.test_result + " " + start.test_cycle);

这实际上会同时运行所有的 jar 文件。

我实际上希望一次执行一个 jar,并且在当前 jar 完成执行后执行下一个 jar。

我添加了以下内容。

p.waitFor();

但是,它的行为仍然相同,即同时执行。

我是否错误地使用了 waitFor() ?感谢您的建议。

更新: 下面是遍历excel表格的代码

public static void main(final String[] args) throws IOException, Throwable {
        final DataFormatter df = new DataFormatter();
        final FileInputStream StartInput = new FileInputStream("c:\\TA\\TestConfig_MA.xlsx");
        final XSSFWorkbook StartInputWB = new XSSFWorkbook(StartInput);
        final XSSFSheet sheet = StartInputWB.getSheet("Config");
        System.out.println("Amount of Row in test config : "+ sheet.getLastRowNum());
        start.count = 1;
        //while (start.count <= sheet.getLastRowNum()) {
        for(start.count = 1; start.count <= sheet.getLastRowNum(); start.count++){  
            System.out.println("Total test case = " + sheet.getLastRowNum());
            System.out.println(start.count);
            final XSSFRow row = sheet.getRow(start.count);
            start.testability = row.getCell(0).toString();            
            start.jar_filepath = row.getCell(1).toString();
            start.tc_name = row.getCell(2).toString();
            start.test_data = row.getCell(3).toString();
            start.test_result = row.getCell(4).toString();
            start.test_cycle = df.formatCellValue(row.getCell(5));
            System.out.println("test cycle from start.jar = " + start.test_cycle);
            System.out.println("Test Case Name : " + start.tc_name);
            if (start.testability.equals("Y") || start.testability.equals("y)")) {
                System.out.println("Test Case Name : " + start.tc_name);
                System.out.println("Round : " + start.count);
                final Process p = Runtime.getRuntime().exec("cmd /c start cmd /k java -jar " + start.jar_filepath + " " + start.tc_name + " " + start.test_data + " " + start.test_result + " " + start.test_cycle);
                p.waitFor();
                System.out.println("wait for = " +p.waitFor());
            else {
                System.out.println("Its a no");
            }
           // ++start.count;
        }//for
        System.out.println("test is done");
    }
   
    }

【问题讨论】:

  • 您能否提供一个minimal reproducible example,您可以在其中展示您如何循环遍历 excel 行、您如何等待以及如何处理异常。目前我们无法告诉您您是否做错了什么,因为我们没有足够的信息
  • @Lino 很高兴。让我补充一下主要问题

标签: java process wait


【解决方案1】:

为什么需要"cmd /c start cmd /k ..."? 你运行cmd 运行start 运行cmd 运行java

特别是start 打开一个新窗口并立即返回。因此p.waitFor(); 将等待start 完成,而不是等待它打开的新窗口。

您可能希望将其缩小为简单

...exec("java -jar " + ...)

至少

...exec("cmd /c java -jar " + ...)

【讨论】:

  • 你能告诉我如何将 ```` p.waitFor() ```` 分配给打开的窗口吗?
  • 我已经尝试了您的建议,但我无法运行 jar 文件。请建议我如何与您分享我从 appium 获得的日志文件。另外,我还需要打开 cmd 窗口。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-29
  • 2011-06-09
  • 2010-10-16
相关资源
最近更新 更多