APIdia Launch Partner
Top Description Inners Methods
java.lang

public Interface ProcessHandle

extends Comparable<ProcessHandle>
Known Direct Implementers
java.lang.ProcessHandleImpl
Imports
java.time.Duration, .Instant, java.util.Optional, java.util.concurrent.CompletableFuture, java.util.stream.Stream

ProcessHandle identifies and provides control of native processes. Each individual process can be monitored for liveness, list its children, get information about the process or destroy it. By comparison, 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.util.concurrent.CompletableFuture 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.lang.UnsupportedOperationException. The methods of this class throw java.lang.UnsupportedOperationException 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.

Since
9
See Also
Process

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
ProcessHandle.Info

Information snapshot about the process.

Method Summary

Modifier and TypeMethod and Description
public static Stream<ProcessHandle>

Returns:

a Stream of ProcessHandles for all processes
allProcesses
()

Returns a snapshot of all processes visible to the current process.

public Stream<ProcessHandle>

Returns:

a sequential Stream of ProcessHandles for processes that are direct children of the process
children
()

Returns 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.
compareTo
(ProcessHandle
the ProcessHandle to be compared
other
)

Redeclares java.lang.Comparable.compareTo.

Compares this ProcessHandle with the specified ProcessHandle for order.

public static ProcessHandle

Returns:

a ProcessHandle for the current process
current
()

Returns a ProcessHandle for the current process.

public Stream<ProcessHandle>

Returns:

a sequential Stream of ProcessHandles for processes that are descendants of the process
descendants
()

Returns a snapshot of the descendants of the process.

public boolean

Returns:

true if termination was successfully requested, otherwise false
destroy
()

Requests the process to be killed.

public boolean

Returns:

true if termination was successfully requested, otherwise false
destroyForcibly
()

Requests 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 false
equals
(Object
another object
other
)

Returns true if other object is non-null, is of the same implementation, and represents the same system process; otherwise it returns false.

public int

Returns:

a hash code value for this object
hashCode
()

Returns a hash code value for this ProcessHandle.

public ProcessHandle.Info

Returns:

a snapshot of information about the process, always non-null
info
()

Returns a snapshot of information about the process.

public boolean

Returns:

true if the process represented by this ProcessHandle object has not yet terminated
isAlive
()

Tests whether the process represented by this ProcessHandle is alive.

public static Optional<ProcessHandle>

Returns:

an Optional<ProcessHandle> of the PID for the process; the Optional is empty if the process does not exist
of
(long
a native process ID
pid
)

Returns an Optional<ProcessHandle> for an existing native process.

public CompletableFuture<ProcessHandle>

Returns:

a new CompletableFuture<ProcessHandle> for the ProcessHandle
onExit
()

Returns a CompletableFuture<ProcessHandle> for the termination of the process.

public Optional<ProcessHandle>

Returns:

an Optional<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 limitations
parent
()

Returns an Optional<ProcessHandle> for the parent process.

public long

Returns:

the native process ID of the process
pid
()

Returns the native process ID of the process.

public boolean

Returns:

true if the implementation of destroy normally terminates the process; otherwise, destroy forcibly terminates the process
supportsNormalTermination
()

Returns true if the implementation of destroy normally terminates the process.

Method Detail

allProcessesback to summary
public static Stream<ProcessHandle> allProcesses()

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.

Returns:Stream<ProcessHandle>

a Stream of ProcessHandles for all processes

Exceptions
UnsupportedOperationException:
if the implementation does not support this operation
childrenback to summary
public Stream<ProcessHandle> children()

Returns a snapshot of the current direct children of the process. The parent of a direct child process is the process. Typically, a process that is not alive has no children.

Note that processes are created and terminate asynchronously. There is no guarantee that a process is alive.

Returns:Stream<ProcessHandle>

a sequential Stream of ProcessHandles for processes that are direct children of the process

compareToback to summary
public int compareTo(ProcessHandle other)

Redeclares java.lang.Comparable.compareTo.

Compares this ProcessHandle with the specified ProcessHandle for order. The order is not specified, but is consistent with Object#equals, which returns true if and only if two instances of ProcessHandle are of the same implementation and represent the same system process. Comparison is only supported among objects of same implementation. If attempt is made to mutually compare two different implementations of ProcessHandles, ClassCastException is thrown.

Parameters
other:ProcessHandle

the ProcessHandle to be compared

Returns:int

a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Annotations
@Override
Exceptions
NullPointerException:
if the specified object is null
ClassCastException:
if the specified object is not of same class as this object
currentback to summary
public static ProcessHandle current()

Returns a ProcessHandle for the current process. The ProcessHandle cannot be used to destroy the current process, use System.exit instead.

Returns:ProcessHandle

a ProcessHandle for the current process

Exceptions
UnsupportedOperationException:
if the implementation does not support this operation
descendantsback to summary
public Stream<ProcessHandle> descendants()

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 not alive has no children.

Note that processes are created and terminate asynchronously. There is no guarantee that a process is alive.

Returns:Stream<ProcessHandle>

a sequential Stream of ProcessHandles for processes that are descendants of the process

destroyback to summary
public boolean destroy()

Requests the process to be killed. Whether the process represented by this ProcessHandle object is normally terminated or not is implementation dependent. Forcible process destruction is defined as the immediate termination of the process, whereas normal termination allows the process to shut down cleanly. If the process is not alive, no action is taken. The operating system access controls may prevent the process from being killed.

The java.util.concurrent.CompletableFuture from onExit is completed when the process has terminated.

Note

The process may not terminate immediately. For example, isAlive() may return true for a brief period after destroy() is called.

Returns:boolean

true if termination was successfully requested, otherwise false

Exceptions
IllegalStateException:
if the process is the current process
destroyForciblyback to summary
public boolean destroyForcibly()

Requests the process to be killed forcibly. The process represented by this ProcessHandle object is forcibly terminated. Forcible process destruction is defined as the immediate termination of the process, whereas normal termination allows the process to shut down cleanly. If the process is not alive, no action is taken. The operating system access controls may prevent the process from being killed.

The java.util.concurrent.CompletableFuture from onExit is completed when the process has terminated.

Note

The process may not terminate immediately. For example, isAlive() may return true for a brief period after destroyForcibly() is called.

Returns:boolean

true if termination was successfully requested, otherwise false

Exceptions
IllegalStateException:
if the process is the current process
equalsback to summary
public boolean equals(Object other)

Returns true if other object is non-null, is of the same implementation, and represents the same system process; otherwise it returns false.

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 equals should return true for two ProcessHandles with the same PID unless there is information to distinguish them.

Parameters
other:Object

another object

Returns:boolean

true if the other object is non-null, is of the same implementation class and represents the same system process; otherwise returns false

Annotations
@Override
hashCodeback to summary
public int hashCode()

Returns a hash code value for this ProcessHandle. The hashcode value follows the general contract for Object#hashCode(). The value is a function of the pid() value and may be a function of additional information to uniquely identify the process. If two ProcessHandles are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.

Returns:int

a hash code value for this object

Annotations
@Override
infoback to summary
public ProcessHandle.Info info()

Returns a snapshot of information about the process.

A ProcessHandle.Info instance has accessor methods that return information about the process if it is available.

Returns:ProcessHandle.Info

a snapshot of information about the process, always non-null

isAliveback to summary
public boolean isAlive()

Tests whether the process represented by this ProcessHandle is alive. Process termination is implementation and operating system specific. The process is considered alive as long as the PID is valid.

Returns:boolean

true if the process represented by this ProcessHandle object has not yet terminated

ofback to summary
public static Optional<ProcessHandle> of(long pid)

Returns an Optional<ProcessHandle> for an existing native process.

Parameters
pid:long

a native process ID

Returns:Optional<ProcessHandle>

an Optional<ProcessHandle> of the PID for the process; the Optional is empty if the process does not exist

Exceptions
UnsupportedOperationException:
if the implementation does not support this operation
onExitback to summary
public CompletableFuture<ProcessHandle> onExit()

Returns a CompletableFuture<ProcessHandle> for the termination of the process. The java.util.concurrent.CompletableFuture provides the ability to trigger dependent functions or actions that may be run synchronously or asynchronously upon process termination. When the process has terminated the CompletableFuture is completed regardless of the exit status of the process. The onExit method can be called multiple times to invoke independent actions when the process exits.

Calling onExit().get() waits for the process to terminate and returns the ProcessHandle. The future can be used to check if the process is done or to wait for it to terminate. Cancelling the CompletableFuture does not affect the Process.

API Note

The process may be observed to have terminated with isAlive before the CompletableFuture is completed and dependent actions are invoked.

Returns:CompletableFuture<ProcessHandle>

a new CompletableFuture<ProcessHandle> for the ProcessHandle

Exceptions
IllegalStateException:
if the process is the current process
parentback to summary
public Optional<ProcessHandle> parent()

Returns an Optional<ProcessHandle> for the parent process. Note that Processes in a zombie state usually don't have a parent.

Returns:Optional<ProcessHandle>

an Optional<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 limitations

pidback 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 equals or compareTo to compare ProcessHandles.

Returns:long

the native process ID of the process

Exceptions
UnsupportedOperationException:
if the implementation does not support this operation
supportsNormalTerminationback to summary
public boolean supportsNormalTermination()

Returns true if the implementation of destroy normally terminates the process. Returns false if the implementation of destroy forcibly and immediately terminates the process.

Returns:boolean

true if the implementation of destroy normally terminates the process; otherwise, destroy forcibly terminates the process

java.lang back to summary

public Interface ProcessHandle.Info

Known Direct Implementers
java.lang.ProcessHandleImpl.Info

Information snapshot about the process. The attributes of a process vary by operating system and are not available in all implementations. Information about processes is limited by the operating system privileges of the process making the request. The return types are Optional<T> allowing explicit tests and actions if the value is available.
Since
9

Method Summary

Modifier and TypeMethod and Description
public Optional<String[]>

Returns:

an Optional<String[]> of the arguments of the process
arguments
()

Returns an array of Strings of the arguments of the process.

public Optional<String>

Returns:

an Optional<String> of the executable pathname of the process
command
()

Returns the executable pathname of the process.

public Optional<String>

Returns:

an Optional<String> of the command line of the process
commandLine
()

Returns the command line of the process.

public Optional<Instant>

Returns:

an Optional<Instant> of the start time of the process
startInstant
()

Returns the start time of the process.

public Optional<Duration>

Returns:

an Optional<Duration> for the accumulated total cputime
totalCpuDuration
()

Returns the total cputime accumulated of the process.

public Optional<String>

Returns:

an Optional<String> for the user of the process
user
()

Return the user of the process.

Method Detail

argumentsback to summary
public Optional<String[]> arguments()

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.

Returns:Optional<String[]>

an Optional<String[]> of the arguments of the process

commandback to summary
public Optional<String> command()

Returns the executable pathname of the process.

Returns:Optional<String>

an Optional<String> of the executable pathname of the process

commandLineback to summary
public Optional<String> commandLine()

Returns the command line of the process.

If command() and arguments() return non-empty optionals, this is simply a convenience method which concatenates the values of the two functions separated by spaces. Otherwise, it will return a best-effort, platform dependent representation of the command line.

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.

Returns:Optional<String>

an Optional<String> of the command line of the process

startInstantback to summary
public Optional<Instant> startInstant()

Returns the start time of the process.

Returns:Optional<Instant>

an Optional<Instant> of the start time of the process

totalCpuDurationback to summary
public Optional<Duration> totalCpuDuration()

Returns the total cputime accumulated of the process.

Returns:Optional<Duration>

an Optional<Duration> for the accumulated total cputime

userback to summary
public Optional<String> user()

Return the user of the process.

Returns:Optional<String>

an Optional<String> for the user of the process