Home > Java > javaTutorial > How to get all child processes of a process using Process API in Java 9?

How to get all child processes of a process using Process API in Java 9?

WBOY
Release: 2023-09-09 10:41:02
forward
757 people have browsed it

如何使用Java 9中的Process API获取进程的所有子进程?

Process Processing classes and related APIs were introduced in Java 9. We can use the ProcessHandle interface and related methods to obtain pid and other related process information. We need to get all the children of the process and then use the java.lang.ProcessHandle.children() method. This method returns a stream, usually a process without child processes.

In the example below, we can get the first process and retrieve its child process information.

Example

import java.util.stream.Stream;
import java.util.Optional;

public class ChilderenProcessTest {
   public static void main(String args[]) throws InterruptedException {
      System.out.println("---------------------------");
      System.out.println("Children Processes:");
      <strong>Optional<ProcessHandle></strong> processHandle = <strong>ProcessHandle.allProcesses().findFirst()</strong>;
      processHandle.<strong>ifPresent</strong>(proc -> proc.<strong>children()</strong>.forEach(child -> System.out.println("PID: [ " + <strong>child.pid()</strong> + " ], Cmd: [ " + <strong>child.info().command()</strong> + " ]")));
   }
}
Copy after login

Output

<strong>---------------------------
Children Processes:
PID: [ 0 ], Cmd: [ Optional.empty ]
PID: [ 4 ], Cmd: [ Optional.empty ]
PID: [ 424 ], Cmd: [ Optional.empty ]
PID: [ 504 ], Cmd: [ Optional.empty ]
PID: [ 560 ], Cmd: [ Optional.empty ]
PID: [ 444 ], Cmd: [ Optional.empty ]
PID: [ 1236 ], Cmd: [ Optional.empty ]
PID: [ 1288 ], Cmd: [ Optional.empty ]
PID: [ 1408 ], Cmd: [ Optional.empty ]
PID: [ 1424 ], Cmd: [ Optional.empty ]
PID: [ 1452 ], Cmd: [ Optional.empty ]
PID: [ 1468 ], Cmd: [ Optional.empty ]
PID: [ 5412 ], Cmd: [ Optional[C:\WINDOWS\System32\taskhostex.exe] ]
PID: [ 3760 ], Cmd: [ Optional[C:\Program Files\Synaptics\SynTP\SynTPEnh.exe] ]
PID: [ 5216 ], Cmd: [ Optional[C:\WINDOWS\explorer.exe] ]
PID: [ 2460 ], Cmd: [ Optional[C:\Program Files (x86)\Dell Wireless\Bluetooth Suite\BtvStack.exe] ]
PID: [ 6064 ], Cmd: [ Optional[C:\Program Files\Realtek\Audio\HDA\RtkNGUI64.exe] ]
PID: [ 7172 ], Cmd: [ Optional[C:\Program Files (x86)\Google\Chrome\Application\chrome.exe] ]
PID: [ 860 ], Cmd: [ Optional[C:\Program Files (x86)\Google\Chrome\Application\chrome.exe] ]
PID: [ 9000 ], Cmd: [ Optional.empty ]
PID: [ 4180 ], Cmd: [ Optional[C:\WINDOWS\System32\cmd.exe] ]
PID: [ 3748 ], Cmd: [ Optional[C:\WINDOWS\System32\conhost.exe] ]
PID: [ 3376 ], Cmd: [ Optional.empty ]
PID: [ 2548 ], Cmd: [ Optional.empty ]
PID: [ 1820 ], Cmd: [ Optional[C:\Program Files\Java\jdk-9.0.4\bin\java.exe] ]</strong>
Copy after login

The above is the detailed content of How to get all child processes of a process using Process API in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template