英文:
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("cmd /c start cmd /k java -jar " + start.jar_filepath + " " + start.tc_name + " " + start.test_data + " " + start.test_result + " " + 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("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");
}
}
答案1
得分: 1
为什么需要 "cmd /c start cmd /k ..."
?
你运行了 cmd
,它运行了 start
,start
又运行了 cmd
,cmd
最终运行了 java
。
特别地,start
打开一个新窗口并立即返回。因此,p.waitFor();
将等待 start
完成,但不会等待它所打开的新窗口完成。
你可能想要简化为:
...exec("java -jar " + ...)
或者至少:
...exec("cmd /c java -jar " + ...)
英文:
why you need "cmd /c start cmd /k ..."
?
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("java -jar " + ...)
or at least
...exec("cmd /c java -jar " + ...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论