How to kill a child process started by process in java?

java, process

Solution

In Java 7 ProcessBuilder can do the redirection for you, so just run iperf directly rather than through `cmd.exe`.

ProcessBuilder pb = new ProcessBuilder("iperf", "-s");
pb.redirectOutput(new File("testresult.txt"));
Process p = pb.start();

The resulting `p` is now itext itself, so `destroy()` will work as you require.

Problem

In the below code snippet, if I destroy `Process p` using `p.destroy()` only process `p`(i.e.`cmd.exe`) is getting destroyed. But not its child `iperf.exe`. How to terminate this process in Java. ``` Process p= Runtime.getRuntime().exec("cmd /c iperf -s > testresult.txt"); ```

Original source