0% found this document useful (0 votes)
9 views5 pages

Java - Process API Improvements

Java 9 has significantly improved the Process API, allowing better control and management of operating system processes through the ProcessHandle class, which provides detailed process information and methods for process management. Examples demonstrate how to spawn a new process, retrieve current process information, and access child processes using the ProcessHandle class. The document includes code snippets illustrating these functionalities and their outputs.

Uploaded by

codespeedcode
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Java - Process API Improvements

Java 9 has significantly improved the Process API, allowing better control and management of operating system processes through the ProcessHandle class, which provides detailed process information and methods for process management. Examples demonstrate how to spawn a new process, retrieve current process information, and access child processes using the ProcessHandle class. The document includes code snippets illustrating these functionalities and their outputs.

Uploaded by

codespeedcode
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Home Java Java - Process API Improvements

Java - Process API Improvements

In Java 9 Process API which is responsible to control and manage operating system
processes has been improved considerably. ProcessHandle Class now provides process's
native process ID, start time, accumulated CPU time, arguments, command, user, parent
process, and descendants. ProcessHandle class also provides method to check processes'
liveness and to destroy processes. It has onExit method, the CompletableFuture class can
perform action asynchronously when process exits.

Spawning a new Process Example


In this example, we've created a new Process for notepad and started it using
ProcessBuilder. Using [Link] interface, we're getting the process information
of the newly spawned process.

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];

public class Tester {


public static void main(String[] args) throws IOException {
ProcessBuilder pb = new ProcessBuilder("[Link]");
String np = "Not Present";
Process p = [Link]();
[Link] info = [Link]();
[Link]("Process ID : %s%n", [Link]());
[Link]("Command name : %s%n", [Link]().orElse(np));
[Link]("Command line : %s%n",
[Link]().orElse(np));

[Link]("Start time: %s%n",


[Link]().map(i -> [Link]([Link]())
.toLocalDateTime().toString()).orElse(np));

[Link]("Arguments : %s%n",
[Link]().map(a -> [Link](a).collect(
[Link](" "))).orElse(np));

[Link]("User : %s%n", [Link]().orElse(np));


}
}

Output

You will see the similar output.

Process ID : 5580
Command name : C:\Program
Files\WindowsApps\Microsoft.WindowsNotepad_11.2401.26.0_x64__8wekyb3d8bbwe\Note
pad\[Link]
Command line : Not Present
Start time: 2024-04-02T17:07:14.305
Arguments : Not Present
User : DESKTOP\Tutorialspoint

Getting Current Process Information Example


In this example, we've getting current process using [Link]() method.
Then using [Link] interface, we're getting the process information of the
current process.

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
public class Tester {
public static void main(String[] args) throws IOException {
String np = "Not Present";
ProcessHandle currentProcess = [Link]();
[Link] info = [Link]();
[Link]("Process ID : %s%n", [Link]());
[Link]("Command name : %s%n", [Link]().orElse(np));
[Link]("Command line : %s%n",
[Link]().orElse(np));

[Link]("Start time: %s%n",


[Link]().map(i -> [Link]([Link]())
.toLocalDateTime().toString()).orElse(np));

[Link]("Arguments : %s%n",
[Link]().map(a -> [Link](a).collect(
[Link](" "))).orElse(np));

[Link]("User : %s%n", [Link]().orElse(np));


}
}

Output

You will see the similar output.

Process ID : 5352
Command name : C:\Program Files\Java\jdk-21\bin\[Link]
Command line : Not Present
Start time: 2024-04-02T17:09:17.902
Arguments : Not Present
User : DESKTOP-DTHL8BI\Tutorialspoint

Getting Child Processes Example


In this example, we've getting current process chid processes using
[Link]().children() method. Then using [Link] interface,
we're getting the process information of the child processes.
package [Link];

import [Link];
import [Link];

public class Tester {


public static void main(String[] args) throws IOException {
for (int i = 0; i < 3; i++) {
ProcessBuilder processBuilder
= new ProcessBuilder("[Link]", "-version");
[Link]().start();
}
Stream<ProcessHandle> childProcesses =
[Link]().children();
String np = "Not Present";
[Link](ProcessHandle::isAlive).forEach(
childProcess ->{
[Link]("Process ID : %s%n", [Link]());
[Link]("Command name : %s%n",
[Link]().command().orElse(np));
[Link]("Command line : %s%n",
[Link]().commandLine().orElse(np));
}
);
}
}

Output

You will see the similar output.

Process ID : 5420
Command name : C:\Program Files\Java\jdk-21\bin\[Link]
Command line : Not Present
Process ID : 15796
Command name : C:\Program Files\Java\jdk-21\bin\[Link]
Command line : Not Present
Process ID : 14180
Command name : C:\Program Files\Java\jdk-21\bin\[Link]
Command line : Not Present
java version "21.0.2" 2024-01-16 LTS
Java(TM) SE Runtime Environment (build 21.0.2+13-LTS-58)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.2+13-LTS-58, mixed mode, sharing)
java version "21.0.2" 2024-01-16 LTS
Java(TM) SE Runtime Environment (build 21.0.2+13-LTS-58)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.2+13-LTS-58, mixed mode, sharing)
java version "21.0.2" 2024-01-16 LTS
Java(TM) SE Runtime Environment (build 21.0.2+13-LTS-58)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.2+13-LTS-58, mixed mode, sharing)

You might also like