APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.lang

pack-priv final Class ProcessImpl

extends Process
Class Inheritance
Imports
java.lang.ProcessBuilder.Redirect, java.io.BufferedInputStream, .BufferedOutputStream, .ByteArrayInputStream, .FileDescriptor, .FileInputStream, .FileOutputStream, .IOException, .InputStream, .OutputStream, java.nio.charset.Charset, java.util.Arrays, .Locale, java.util.concurrent.CompletableFuture, .TimeUnit, java.util.concurrent.locks.Condition, .ReentrantLock, jdk.internal.access.JavaIOFileDescriptorAccess, .SharedSecrets, jdk.internal.util.OperatingSystem, .StaticProperty

java.lang.Process subclass in the UNIX environment.
Authors
Mario Wolczko and Ross Knippel., Konstantin Kladko (ported to Linux and Bsd), Martin Buchholz, Volker Simonis (ported to AIX)
Since
1.5

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ProcessImpl.DeferredCloseProcessPipeInputStream

A buffered input stream for a subprocess pipe file descriptor that allows the underlying file descriptor to be reclaimed when the process exits, via the processExited hook.

private static enum
private static class
ProcessImpl.ProcessPipeInputStream

A buffered input stream for a subprocess pipe file descriptor that allows the underlying file descriptor to be reclaimed when the process exits, via the processExited hook.

private static class
ProcessImpl.ProcessPipeOutputStream

A buffered output stream for a subprocess pipe file descriptor that allows the underlying file descriptor to be reclaimed when the process exits, via the processExited hook.

Field Summary

Modifier and TypeField and Description
private final Condition
private int
private static final JavaIOFileDescriptorAccess
private boolean
private static final byte[]
pack-priv static final Charset
private static final ProcessImpl.LaunchMechanism
private final ReentrantLock
private final int
private final ProcessHandleImpl
private InputStream
private OutputStream
private InputStream
pack-priv static final boolean

Constructor Summary

AccessConstructor and Description
private
ProcessImpl(final byte[] prog, final byte[] argBlock, final int argc, final byte[] envBlock, final int envc, final byte[] dir, final int[] fds, final boolean forceNullOutputStream, final boolean redirectErrorStream)

Method Summary

Modifier and TypeMethod and Description
private void
destroy(boolean force)

public void
destroy()

Implements abstract java.lang.Process.destroy.

Kills the process.

public Process
destroyForcibly()

Overrides java.lang.Process.destroyForcibly.

Kills the process forcibly.

public int
exitValue()

Implements abstract java.lang.Process.exitValue.

Returns the exit value for the process.

private native int

Returns:

the pid of the subprocess
forkAndExec
(int mode, byte[] helperpath, byte[] prog, byte[] argBlock, int argc, byte[] envBlock, int envc, byte[] dir, int[]
an array of three file descriptors. Indexes 0, 1, and 2 correspond to standard input, standard output and standard error, respectively. On input, a value of -1 means to create a pipe to connect child and parent processes. On output, a value which is not -1 is the parent pipe fd corresponding to the pipe which has been created. An element of this array is -1 on input if and only if it is not -1 on output.
fds
,
boolean redirectErrorStream)

Creates a process.

public InputStream
getErrorStream()

Implements abstract java.lang.Process.getErrorStream.

Returns the input stream connected to the error output of the process.

public InputStream
getInputStream()

Implements abstract java.lang.Process.getInputStream.

Returns the input stream connected to the normal output of the process.

public OutputStream
getOutputStream()

Implements abstract java.lang.Process.getOutputStream.

Returns the output stream connected to the normal input of the process.

private static native void
init()

pack-priv void
initStreams(int[]
array of stdin, stdout, stderr fds
fds
,
boolean
true if the stdout is being directed to a subsequent process. The stdout stream should be a null output stream .
forceNullOutputStream
)

Initialize the streams from the file descriptors.

public boolean
isAlive()

Overrides java.lang.Process.isAlive.

Tests whether the process represented by this Process is alive.

private static ProcessImpl.LaunchMechanism

Returns:

the default or requested launch mechanism
launchMechanism
()

Returns the default or requested launch mechanism.

pack-priv static FileDescriptor
public CompletableFuture<Process>
onExit()

Overrides java.lang.Process.onExit.

Returns a CompletableFuture<Process> for the termination of the Process.

public long
pid()

Overrides java.lang.Process.pid.

Returns the native process ID of the process.

pack-priv static Process
start(String[] cmdarray, Map<String, String> environment, String dir, ProcessBuilder.Redirect[] redirects, boolean redirectErrorStream)

public boolean
supportsNormalTermination()

Overrides java.lang.Process.supportsNormalTermination.

Returns true if the implementation of destroy is to normally terminate the process, Returns false if the implementation of destroy forcibly and immediately terminates the process.

private static byte[]
public ProcessHandle
toHandle()

Overrides java.lang.Process.toHandle.

Returns a ProcessHandle for the Process.

public String

Returns:

a string representation of the object.
toString
()

Overrides java.lang.Object.toString.

The toString method returns a string consisting of the native process ID of the process and the exit value of the process.

public int
waitFor()

Implements abstract java.lang.Process.waitFor.

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated.

public boolean
waitFor(long timeout, TimeUnit unit)

Overrides java.lang.Process.waitFor.

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated, or the specified waiting time elapses.

Inherited from java.lang.Process:
childrenclosedescendantserrorReadererrorReaderinfoinputReaderinputReaderoutputWriteroutputWriterwaitFor

Field Detail

conditionback to summary
private final Condition condition
exitcodeback to summary
private int exitcode
fdAccessback to summary
private static final JavaIOFileDescriptorAccess fdAccess
hasExitedback to summary
private boolean hasExited
helperpathback to summary
private static final byte[] helperpath
JNU_CHARSETback to summary
pack-priv static final Charset JNU_CHARSET
launchMechanismback to summary
private static final ProcessImpl.LaunchMechanism launchMechanism
lockback to summary
private final ReentrantLock lock
pidback to summary
private final int pid
processHandleback to summary
private final ProcessHandleImpl processHandle
stderrback to summary
private InputStream stderr
stdinback to summary
private OutputStream stdin
stdoutback to summary
private InputStream stdout
SUPPORTS_NORMAL_TERMINATIONback to summary
pack-priv static final boolean SUPPORTS_NORMAL_TERMINATION

Constructor Detail

ProcessImplback to summary
private ProcessImpl(final byte[] prog, final byte[] argBlock, final int argc, final byte[] envBlock, final int envc, final byte[] dir, final int[] fds, final boolean forceNullOutputStream, final boolean redirectErrorStream) throws IOException

Method Detail

destroyback to summary
private void destroy(boolean force)
destroyback to summary
public void destroy()

Implements abstract java.lang.Process.destroy.

Doc from java.lang.Process.destroy.

Kills the process. Whether the process represented by this Process object is normally terminated or not is implementation dependent. Forcible process destruction is defined as the immediate termination of a process, whereas normal termination allows the process to shut down cleanly. If the process is not alive, no action is taken.

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

Annotations
@Override
destroyForciblyback to summary
public Process destroyForcibly()

Overrides java.lang.Process.destroyForcibly.

Doc from java.lang.Process.destroyForcibly.

Kills the process forcibly. The process represented by this Process object is forcibly terminated. Forcible process destruction is defined as the immediate termination of a process, whereas normal termination allows the process to shut down cleanly. If the process is not alive, no action is taken.

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

Invoking this method on Process objects returned by ProcessBuilder#start() and Runtime#exec forcibly terminate the process.

Annotations
@Override
exitValueback to summary
public int exitValue()

Implements abstract java.lang.Process.exitValue.

Doc from java.lang.Process.exitValue.

Returns the exit value for the process.

forkAndExecback to summary
private native int forkAndExec(int mode, byte[] helperpath, byte[] prog, byte[] argBlock, int argc, byte[] envBlock, int envc, byte[] dir, int[] fds, boolean redirectErrorStream) throws IOException

Creates a process. Depending on the mode flag, this is done by one of the following mechanisms:

  1 - fork(2) and exec(2)
  2 - posix_spawn(3P)
Parameters
fds:int[]

an array of three file descriptors. Indexes 0, 1, and 2 correspond to standard input, standard output and standard error, respectively. On input, a value of -1 means to create a pipe to connect child and parent processes. On output, a value which is not -1 is the parent pipe fd corresponding to the pipe which has been created. An element of this array is -1 on input if and only if it is not -1 on output.

Returns:int

the pid of the subprocess

getErrorStreamback to summary
public InputStream getErrorStream()

Implements abstract java.lang.Process.getErrorStream.

Doc from java.lang.Process.getErrorStream.

Returns the input stream connected to the error output of the process. The stream obtains data piped from the error output of the process represented by this Process object.

If the standard error of the process has been redirected using ProcessBuilder.redirectError or ProcessBuilder.redirectErrorStream then this method will return a null input stream.

The error stream should be closed when it is no longer needed.

getInputStreamback to summary
public InputStream getInputStream()

Implements abstract java.lang.Process.getInputStream.

Doc from java.lang.Process.getInputStream.

Returns the input stream connected to the normal output of the process. The stream obtains data piped from the standard output of the process represented by this Process object.

If the standard output of the process has been redirected using ProcessBuilder.redirectOutput then this method will return a null input stream.

Otherwise, if the standard error of the process has been redirected using ProcessBuilder.redirectErrorStream then the input stream returned by this method will receive the merged standard output and the standard error of the process.

The input stream should be closed when it is no longer needed.

getOutputStreamback to summary
public OutputStream getOutputStream()

Implements abstract java.lang.Process.getOutputStream.

Doc from java.lang.Process.getOutputStream.

Returns the output stream connected to the normal input of the process. Output to the stream is piped into the standard input of the process represented by this Process object.

If the standard input of the process has been redirected using ProcessBuilder.redirectInput then this method will return a null output stream.

The output stream should be closed when it is no longer needed.

initback to summary
private static native void init()
initStreamsback to summary
pack-priv void initStreams(int[] fds, boolean forceNullOutputStream) throws IOException

Initialize the streams from the file descriptors.

Parameters
fds:int[]

array of stdin, stdout, stderr fds

forceNullOutputStream:boolean

true if the stdout is being directed to a subsequent process. The stdout stream should be a null output stream .

isAliveback to summary
public boolean isAlive()

Overrides java.lang.Process.isAlive.

Doc from java.lang.Process.isAlive.

Tests whether the process represented by this Process is alive.

Annotations
@Override
launchMechanismback to summary
private static ProcessImpl.LaunchMechanism launchMechanism()

Returns the default or requested launch mechanism.

Returns:ProcessImpl.LaunchMechanism

the default or requested launch mechanism

Exceptions
Error:
if the requested launch mechanism is not found or valid
newFileDescriptorback to summary
pack-priv static FileDescriptor newFileDescriptor(int fd)
onExitback to summary
public CompletableFuture<Process> onExit()

Overrides java.lang.Process.onExit.

Doc from java.lang.Process.onExit.

Returns a CompletableFuture<Process> 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.

Calling onExit().get() waits for the process to terminate and returns the Process. 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.

Processes returned from ProcessBuilder#start() override the default implementation to provide an efficient mechanism to wait for process exit.

Annotations
@Override
pidback to summary
public long pid()

Overrides java.lang.Process.pid.

Doc from java.lang.Process.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.

Annotations
@Override
startback to summary
pack-priv static Process start(String[] cmdarray, Map<String, String> environment, String dir, ProcessBuilder.Redirect[] redirects, boolean redirectErrorStream) throws IOException
supportsNormalTerminationback to summary
public boolean supportsNormalTermination()

Overrides java.lang.Process.supportsNormalTermination.

Doc from java.lang.Process.supportsNormalTermination.

Returns true if the implementation of destroy is to normally terminate the process, Returns false if the implementation of destroy forcibly and immediately terminates the process.

Invoking this method on Process objects returned by ProcessBuilder#start() and Runtime#exec return true or false depending on the platform implementation.

Annotations
@Override
toCStringback to summary
private static byte[] toCString(String s)
toHandleback to summary
public ProcessHandle toHandle()

Overrides java.lang.Process.toHandle.

Doc from java.lang.Process.toHandle.

Returns a ProcessHandle for the Process. Process objects returned by ProcessBuilder#start() and Runtime#exec implement toHandle as the equivalent of ProcessHandle.of(pid).

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

The toString method returns a string consisting of the native process ID of the process and the exit value of the process.

Returns:String

a string representation of the object.

Annotations
@Override
waitForback to summary
public int waitFor() throws InterruptedException

Implements abstract java.lang.Process.waitFor.

Doc from java.lang.Process.waitFor.

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the process has already terminated. If the process has not yet terminated, the calling thread will be blocked until the process exits.

Exceptions
InterruptedException:
if the current thread is interrupted by another thread while it is waiting, then the wait is ended and an InterruptedException is thrown.
waitForback to summary
public boolean waitFor(long timeout, TimeUnit unit) throws InterruptedException

Overrides java.lang.Process.waitFor.

Doc from java.lang.Process.waitFor.

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated, or the specified waiting time elapses.

If the process has already terminated then this method returns immediately with the value true. If the process has not terminated and the timeout value is less than, or equal to, zero, then this method returns immediately with the value false.

Exceptions
InterruptedException:
if the current thread is interrupted while waiting.
java.lang back to summary

private Class ProcessImpl.DeferredCloseProcessPipeInputStream

extends BufferedInputStream
Class Inheritance

A buffered input stream for a subprocess pipe file descriptor that allows the underlying file descriptor to be reclaimed when the process exits, via the processExited hook. This is tricky because we do not want the user-level InputStream to be closed until the user invokes close(), and we need to continue to be able to read any buffered data lingering in the OS pipe buffer. On AIX this is especially tricky, because the 'close()' system call will block if another thread is at the same time blocked in a file operation (e.g. 'read()') on the same file descriptor. We therefore combine 'ProcessPipeInputStream' approach used on Linux and Bsd with the deferring 'close' of InputStream. This means that every potentially blocking operation on the file descriptor increments a counter before it is executed and decrements it once it finishes. The 'close()' operation will only be executed if there are no pending operations. Otherwise it is deferred after the last pending operation has finished.

Field Summary

Modifier and TypeField and Description
private final Object
private boolean
private int
Inherited from java.io.BufferedInputStream:
bufcountmarklimitmarkpospos

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public int
available()

Overrides java.io.BufferedInputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

public void
close()

Overrides java.io.BufferedInputStream.close.

Implements java.io.Closeable.close.

Closes this input stream and releases any system resources associated with the stream.

private InputStream
private void
pack-priv synchronized void
processExited()

Called by the process reaper thread when the process exits.

private void
public int
read()

Overrides java.io.BufferedInputStream.read.

See the general contract of the read method of InputStream.

public int
read(byte[] b)

Overrides java.io.FilterInputStream.read.

Reads up to b.length bytes of data from this input stream into an array of bytes.

public int
read(byte[] b, int off, int len)

Overrides java.io.BufferedInputStream.read.

Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.

public long
skip(long n)

Overrides java.io.BufferedInputStream.skip.

See the general contract of the skip method of InputStream.

Inherited from java.io.BufferedInputStream:
markmarkSupportedresettransferTo

Field Detail

closeLockback to summary
private final Object closeLock
closePendingback to summary
private boolean closePending
useCountback to summary
private int useCount

Constructor Detail

DeferredCloseProcessPipeInputStreamback to summary
pack-priv DeferredCloseProcessPipeInputStream(int fd)

Method Detail

availableback to summary
public int available() throws IOException

Overrides java.io.BufferedInputStream.available.

Doc from java.io.BufferedInputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

This method returns the sum of the number of bytes remaining to be read in the buffer (count - pos) and the result of calling the in.available().

Annotations
@Override
Exceptions
IOException:
if this input stream has been closed by invoking its close() method, or an I/O error occurs.
closeback to summary
public void close() throws IOException

Overrides java.io.BufferedInputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.BufferedInputStream.close.

Closes this input stream and releases any system resources associated with the stream. Once the stream has been closed, further read(), available(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
drainInputStreamback to summary
private InputStream drainInputStream(InputStream in) throws IOException
lowerback to summary
private void lower() throws IOException
processExitedback to summary
pack-priv synchronized void processExited()

Called by the process reaper thread when the process exits.

raiseback to summary
private void raise()
readback to summary
public int read() throws IOException

Overrides java.io.BufferedInputStream.read.

Doc from java.io.BufferedInputStream.read.

See the general contract of the read method of InputStream.

Annotations
@Override
Exceptions
IOException:
if this input stream has been closed by invoking its close() method, or an I/O error occurs.
readback to summary
public int read(byte[] b) throws IOException

Overrides java.io.FilterInputStream.read.

Doc from java.io.FilterInputStream.read.

Reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
readback to summary
public int read(byte[] b, int off, int len) throws IOException

Overrides java.io.BufferedInputStream.read.

Doc from java.io.BufferedInputStream.read.

Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.

This method implements the general contract of the corresponding read method of the InputStream class. As an additional convenience, it attempts to read as many bytes as possible by repeatedly invoking the read method of the underlying stream. This iterated read continues until one of the following conditions becomes true:

  • The specified number of bytes have been read,
  • The read method of the underlying stream returns -1, indicating end-of-file, or
  • The available method of the underlying stream returns zero, indicating that further input requests would block.
If the first read on the underlying stream returns -1 to indicate end-of-file then this method returns -1. Otherwise, this method returns the number of bytes actually read.

Subclasses of this class are encouraged, but not required, to attempt to read as many bytes as possible in the same fashion.

Annotations
@Override
Exceptions
IOException:
if this input stream has been closed by invoking its close() method, or an I/O error occurs.
skipback to summary
public long skip(long n) throws IOException

Overrides java.io.BufferedInputStream.skip.

Doc from java.io.BufferedInputStream.skip.

See the general contract of the skip method of InputStream.

Annotations
@Override
Exceptions
IOException:
if this input stream has been closed by invoking its close() method, in.skip(n) throws an IOException, or an I/O error occurs.
java.lang back to summary

private final Enum ProcessImpl.LaunchMechanism

extends Enum<ProcessImpl.LaunchMechanism>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final ProcessImpl.LaunchMechanism
public static final ProcessImpl.LaunchMechanism

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ProcessImpl.LaunchMechanism
public static ProcessImpl.LaunchMechanism[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

FORKback to summary
public static final ProcessImpl.LaunchMechanism FORK
POSIX_SPAWNback to summary
public static final ProcessImpl.LaunchMechanism POSIX_SPAWN

Constructor Detail

LaunchMechanismback to summary
private LaunchMechanism()

Method Detail

valueOfback to summary
public static ProcessImpl.LaunchMechanism valueOf(String name)
valuesback to summary
public static ProcessImpl.LaunchMechanism[] values()
java.lang back to summary

private Class ProcessImpl.ProcessPipeInputStream

extends BufferedInputStream
Class Inheritance

A buffered input stream for a subprocess pipe file descriptor that allows the underlying file descriptor to be reclaimed when the process exits, via the processExited hook. This is tricky because we do not want the user-level InputStream to be closed until the user invokes close(), and we need to continue to be able to read any buffered data lingering in the OS pipe buffer.

Field Summary

Modifier and TypeField and Description
private final Object
Inherited from java.io.BufferedInputStream:
bufcountmarklimitmarkpospos

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.BufferedInputStream.close.

Implements java.io.Closeable.close.

Closes this input stream and releases any system resources associated with the stream.

private static byte[]
pack-priv synchronized void
processExited()

Called by the process reaper thread when the process exits.

Inherited from java.io.BufferedInputStream:
availablemarkmarkSupportedreadreadresetskiptransferTo

Field Detail

closeLockback to summary
private final Object closeLock

Constructor Detail

ProcessPipeInputStreamback to summary
pack-priv ProcessPipeInputStream(int fd)

Method Detail

closeback to summary
public void close() throws IOException

Overrides java.io.BufferedInputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.BufferedInputStream.close.

Closes this input stream and releases any system resources associated with the stream. Once the stream has been closed, further read(), available(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
drainInputStreamback to summary
private static byte[] drainInputStream(InputStream in) throws IOException
processExitedback to summary
pack-priv synchronized void processExited()

Called by the process reaper thread when the process exits.

java.lang back to summary

private Class ProcessImpl.ProcessPipeOutputStream

extends BufferedOutputStream
Class Inheritance

A buffered output stream for a subprocess pipe file descriptor that allows the underlying file descriptor to be reclaimed when the process exits, via the processExited hook.

Field Summary

Inherited from java.io.BufferedOutputStream:
bufcount

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv synchronized void
processExited()

Called by the process reaper thread when the process exits.

Inherited from java.io.BufferedOutputStream:
flushwritewrite

Constructor Detail

ProcessPipeOutputStreamback to summary
pack-priv ProcessPipeOutputStream(int fd)

Method Detail

processExitedback to summary
pack-priv synchronized void processExited()

Called by the process reaper thread when the process exits.