`Process.waitFor()`不会“等待”当前线程完成。

huangapple go评论71阅读模式
英文:

Process.waitFor() not "waiting" for current thread to finish

问题

以下是翻译好的内容:

我有一个 Excel 表格,其中列出了要执行的 jar 文件列表。为了启动测试,我将运行代码从所述的 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("测试配置中的行数:" + sheet.getLastRowNum());
    start.count = 1;
    //while (start.count <= sheet.getLastRowNum()) {
    for(start.count = 1; start.count <= sheet.getLastRowNum(); start.count++){	
        System.out.println("总测试案例数 = " + 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("从 start.jar 中获取的测试周期 = " + start.test_cycle);
        System.out.println("测试案例名称:" + start.tc_name);
        if (start.testability.equals("Y") || start.testability.equals("y")) {
            System.out.println("测试案例名称:" + start.tc_name);
            System.out.println("循环次数:" + 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("等待结果 = " +p.waitFor());
        else {
            System.out.println("不进行测试");
        }
       // ++start.count;
    }//for
    System.out.println("测试完成");
}
英文:

I have an excel sheet where it has a list of jar files to be executed. To kick start the testing, I will run a code to read from the said excel sheet and execute the jar files in the excel sheet. The code to run those jar files is as follow :

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

This will actually run all the jar files concurrently.

I actually wanted one jar to be executed at one time, and the next jar to be executed AFTER the current jar has finish execution.

I added the following.

p.waitFor();

However, it still behaves the same, that is, it is executed simultaneously.

Am I using the waitFor() wrongly ? Advice is appreciated.

Update:
The following is the code that iterates the excel sheet

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

答案1

得分: 1

为什么需要 &quot;cmd /c start cmd /k ...&quot;
你运行了 cmd,它运行了 startstart 又运行了 cmdcmd 最终运行了 java

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

你可能想要简化为:

...exec(&quot;java -jar &quot; + ...)

或者至少:

...exec(&quot;cmd /c java -jar &quot; + ...)

英文:

why you need &quot;cmd /c start cmd /k ...&quot;?
You run cmd which runs start which runs cmd which runs java

In particular, start opens a new window and returns immediately. Therefore p.waitFor(); will wait for start to complete, but not the new window opened by it.

You may want to narrow it to simple

...exec(&quot;java -jar &quot; + ...)

or at least

...exec(&quot;cmd /c java -jar &quot; + ...)

huangapple
  • 本文由 发表于 2020年10月7日 16:27:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64240149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定