April 2018
Intermediate to advanced
910 pages
33h 21m
English
Listing the descendants is very similar to listing children, but if we call the processHandle.descendants() method then the Stream will contain all the children processes and the children processes of those processes and so on. The following program starts command prompts with command-line arguments so that they also spawn another cmd.exe that terminates:
package packt.mastering.java9.process; import java.io.IOException; import java.util.stream.Collectors; public class DescendantLister { public static void main(String[] args) throws IOException { for (int i = 0; i < 10; i++) { new ProcessBuilder().command("cmd.exe","/K","cmd"). start(); } System.out.println("Number of descendants: " + ProcessHandle.current().descendants().count(); ...