Java Process thread is running parallel, when command is executed directly in Java

77 views Asked by At

When I am running the batch command ping 127.0.0.1 -n 5 > nul (waits 5 seconds) in Java "directly":

public class Test {
          public static void main(String[] args) throws IOException, InterruptedException {
    
              Process testProcess =  Runtime.getRuntime().exec("ping 127.0.0.1 -n 5 > nul");
              testProcess.waitFor();
              System.out.println("foo");
          }
}

the String "foo" prints out instant and not after 5 seconds, even the .waitFor() method should make the thread wait, until the process is finished.

But when I change testProcess to Runtime.getRuntime().exec("C:/Users/Desktop/test.bat") to execute a Batch file, which has only the command ping 127.0.0.1 -n 5 > nul inside it, the .waitFor()method waits until the process is finished. So that the "foo" is printed after 5 seconds.

Why is that so?


EDIT:

I needed to remove > nul (see @Generous Badger 's comment)

0

There are 0 answers