Process.waitFor() 未"waiting"当前线程完成



我有一个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");
}

}

为什么需要"cmd /c start cmd /k ..."?您运行运行cmdstart运行运行cmdjava

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

你可能想把它缩小到简单的

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

或至少

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

最新更新