Process instances were started
by the current process and additionally provide access to the process
input, output, and error streams.
The native process ID is an identification number that the operating system assigns to the process. The range for process id values is dependent on the operating system. For example, an embedded system might use a 16-bit value. Status information about a process is retrieved from the native system and may change asynchronously; processes may be created or terminate spontaneously. The time between when a process terminates and the process id is reused for a new process is unpredictable. Race conditions can exist between checking the status of a process and acting upon it. When using ProcessHandles avoid assumptions about the liveness or identity of the underlying process.
Each ProcessHandle identifies and allows control of a process in the native
system. ProcessHandles are returned from the factory methods current(),
of(long),
children, descendants, parent() and
allProcesses().
The Process instances created by ProcessBuilder can be queried
for a ProcessHandle that provides information about the Process.
ProcessHandle references should not be freely distributed.
A java. available from onExit
can be used to wait for process termination, and possibly trigger dependent
actions.
The ability to control processes may be restricted by the native system, ProcessHandle provides no more access to, or control over, the native process than would be allowed by a native application.
Implementation Specification
In the case where ProcessHandles cannot be supported then the factory
methods must consistently throw java..
The methods of this class throw java.
if the operating system does not allow access to query or kill a process.
The ProcessHandle static factory methods return instances that are
value-based,
immutable and thread-safe. Programmers should treat instances that are
equal as interchangeable and should not
use instances for synchronization, or unpredictable behavior may occur.
For example, in a future release, synchronization may fail.
Use the equals or compareTo methods
to compare ProcessHandles.
Process
| Modifier and Type | Class and Description |
|---|---|
| public static interface | ProcessHandle.
Information snapshot about the process. |
| Modifier and Type | Method and Description |
|---|---|
| public static Stream | Returns: a Stream of ProcessHandles for all processesReturns a snapshot of all processes visible to the current process. |
| public Stream | Returns: a sequential Stream of ProcessHandles for processes that are direct children of the processReturns a snapshot of the current direct children of the process. |
| public int | Returns: a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.the ProcessHandle to be compared other)Redeclares java. Compares this ProcessHandle with the specified ProcessHandle for order. |
| public static ProcessHandle | Returns: a ProcessHandle for the current processReturns a ProcessHandle for the current process. |
| public Stream | Returns: a sequential Stream of ProcessHandles for processes that are descendants of the processReturns a snapshot of the descendants of the process. |
| public boolean | Returns: true if termination was successfully requested,
otherwise falseRequests the process to be killed. |
| public boolean | Returns: true if termination was successfully requested,
otherwise falseRequests the process to be killed forcibly. |
| public boolean | Returns: true if the other object is non-null,
is of the same implementation class and represents
the same system process; otherwise returns falseanother object other)Returns |
| public int | Returns: a hash code value for this objectReturns a hash code value for this ProcessHandle. |
| public ProcessHandle. | Returns: a snapshot of information about the process, always non-nullReturns a snapshot of information about the process. |
| public boolean | Returns: true if the process represented by this
ProcessHandle object has not yet terminatedTests whether the process represented by this |
| public static Optional | Returns: anOptional<ProcessHandle> of the PID for the process;
the Optional is empty if the process does not exista native process ID pid)Returns an |
| public CompletableFuture | Returns: a newCompletableFuture<ProcessHandle> for the ProcessHandleReturns a |
| public Optional | Returns: anOptional<ProcessHandle> of the parent process;
the Optional is empty if the child process does not have a parent
or if the parent is not available, possibly due to operating system limitationsReturns an |
| public long | |
| public boolean | supportsNormalTermination()
Returns |
| allProcesses | back to summary |
|---|---|
| public static Stream Returns a snapshot of all processes visible to the current process. Note that processes are created and terminate asynchronously. There is no guarantee that a process in the stream is alive or that no other processes may have been created since the inception of the snapshot.
| |
| children | back to summary |
|---|---|
| public Stream Returns a snapshot of the current direct children of the process.
The
Note that processes are created and terminate asynchronously.
There is no guarantee that a process is
| |
| compareTo | back to summary |
|---|---|
| public int compareTo(ProcessHandle other) Redeclares java. Compares this ProcessHandle with the specified ProcessHandle for order.
The order is not specified, but is consistent with
| |
| current | back to summary |
|---|---|
| public static ProcessHandle current() Returns a ProcessHandle for the current process. The ProcessHandle cannot be
used to destroy the current process, use
| |
| descendants | back to summary |
|---|---|
| public Stream Returns a snapshot of the descendants of the process.
The descendants of a process are the children of the process
plus the descendants of those children, recursively.
Typically, a process that is
Note that processes are created and terminate asynchronously.
There is no guarantee that a process is
| |
| destroy | back to summary |
|---|---|
| public boolean destroy() Requests the process to be killed.
Whether the process represented by this
The Note The process may not terminate immediately.
For example,
| |
| destroyForcibly | back to summary |
|---|---|
| public boolean destroyForcibly() Requests the process to be killed forcibly.
The process represented by this
The Note The process may not terminate immediately.
For example,
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object other) Returns Implementation Note It is implementation specific whether ProcessHandles with the same PID
represent the same system process. ProcessHandle implementations
should contain additional information to uniquely identify the process.
For example, the start time of the process could be used
to determine if the PID has been re-used.
The implementation of | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Returns a hash code value for this ProcessHandle.
The hashcode value follows the general contract for
| |
| info | back to summary |
|---|---|
| public ProcessHandle. Returns a snapshot of information about the process. A
| |
| isAlive | back to summary |
|---|---|
| public boolean isAlive() Tests whether the process represented by this
| |
| of | back to summary |
|---|---|
| public static Optional Returns an
| |
| onExit | back to summary |
|---|---|
| public CompletableFuture Returns a
Calling API Note The process may be observed to have terminated with
| |
| parent | back to summary |
|---|---|
| public Optional Returns an
| |
| pid | back to summary |
|---|---|
| public long pid() Returns the native process ID of the process. The native process ID is an
identification number that the operating system assigns to the process.
The operating system may reuse the process ID after a process terminates.
Use
| |
| supportsNormalTermination | back to summary |
|---|---|
| public boolean supportsNormalTermination() Returns | |
Optional<T> allowing explicit tests
and actions if the value is available.
| Modifier and Type | Method and Description |
|---|---|
| public Optional | Returns: anOptional<String[]> of the arguments of the processReturns an array of Strings of the arguments of the process. |
| public Optional | Returns: anOptional<String> of the executable pathname
of the processReturns the executable pathname of the process. |
| public Optional | Returns: anOptional<String> of the command line
of the processReturns the command line of the process. |
| public Optional | Returns: anOptional<Instant> of the start time of the processReturns the start time of the process. |
| public Optional | Returns: anOptional<Duration> for the accumulated total cputimeReturns the total cputime accumulated of the process. |
| public Optional |
| arguments | back to summary |
|---|---|
| public Optional Returns an array of Strings of the arguments of the process. API Note On some platforms, native applications are free to change the arguments array after startup and this method may only show the changed values. | |
| command | back to summary |
|---|---|
| public Optional Returns the executable pathname of the process. | |
| commandLine | back to summary |
|---|---|
| public Optional Returns the command line of the process.
If API Note Note that the returned executable pathname and the arguments may be truncated on some platforms due to system limitations. The executable pathname may contain only the name of the executable without the full path information. It is undecidable whether white space separates different arguments or is part of a single argument. | |
| startInstant | back to summary |
|---|---|
| public Optional Returns the start time of the process. | |
| totalCpuDuration | back to summary |
|---|---|
| public Optional Returns the total cputime accumulated of the process. | |
| user | back to summary |
|---|---|
| public Optional Return the user of the process. | |