Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In JDK 10, get the process ID of the JVM running your application with ProcessHandle.current().pid():

long pid = ProcessHandle.current().pid();

Get the current Java app’s process ID

ProcessHandle.current() returns a handle to the process running the current code, and pid() returns that process’s native operating-system ID. Both APIs are in java.lang and have been available since Java 9, so they are available in JDK 10. See Oracle’s JDK 10 ProcessHandle API.

long pid = ProcessHandle.current().pid();
System.out.println("Process ID: " + pid);

The result is a process ID assigned by the operating system, not a Java thread ID or a JVM-internal identifier. Its numeric range depends on the operating system.

Get the ID of a process started by your code

If you already have a Process object for a process launched with ProcessBuilder or Runtime.exec, call pid() on that object instead:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Process process = new ProcessBuilder("some-command").start();
long childPid = process.pid();

This returns the native process ID of the process represented by that Process, which may be a child process rather than the JVM running your application. In JDK 10, Process.pid() obtains the ID through toHandle().pid(). See Oracle’s JDK 10 Process API.

What you need Call What it identifies
The JVM running this code ProcessHandle.current().pid() The current Java application’s operating-system process
A process represented by a Process object process.pid() The operating-system process represented by that object
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use a PID as a temporary identifier

A PID is useful for reporting a process or looking it up in operating-system tools, but it is not a permanent identity. When a process terminates, the operating system may reuse its PID for another process. Process status can also change asynchronously, so a saved PID alone is not proof that the same process is still running.

For more process details, ProcessHandle.Info provides values such as the command, command line, start time, arguments, and user. Availability varies with the operating system and the requesting process’s privileges. These accessors return Optional values; check whether each value is present before using it. See Oracle’s JDK 10 ProcessHandle.Info API.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.