tester ABC
tester ABC

Reputation: 21

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

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

Upvotes: 0

Views: 243

Answers (1)

AterLux
AterLux

Reputation: 4654

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 " + ...)

Upvotes: 1

Related Questions