Class CommandOutputControl
A command is either a subprocess represented by ProcessBuilder or a
tool provided by ToolProvider.
A command is executed synchronously, and the result of its execution is
stored in a CommandOutputControl.Result instance which captures the exit code and any
saved output streams.
Depending on the configuration, it can save the entire output stream, only the first line, or not save the output at all. Stdout and stderr streams can be configured independently.
Output streams can be treated as either byte streams or character streams.
The table below shows how different parameter combinations affect the content
written to streams returned by dumpStdout() and
dumpStderr() for subsequently executed tools, regardless of whether
their output streams are saved, or for subprocesses when the output streams
are saved:
| discardStdout(false) and discardStderr(false) | discardStdout(false) and discardStderr(true) | discardStdout(true) and discardStderr(false) | |
|---|---|---|---|
| redirectStderr(true) and dumpOutput(true) |
dumpStdout(): STDOUT and STDERR interleaved dumpStderr(): unchanged |
dumpStdout(): STDOUT dumpStderr(): unchanged |
dumpStdout(): STDERR; dumpStderr(): unchanged |
| redirectStderr(false) and dumpOutput(true) |
dumpStdout(): STDOUT dumpStderr(): STDERR |
dumpStdout(): STDOUT dumpStderr(): unchanged |
dumpStdout(): unchanged dumpStderr(): STDERR |
The table below shows how different parameter combinations affect the content
written to the native file descriptors associated with System.out and
System.err for subsequently executed subprocesses when the output
streams are not saved:
| discardStdout(false) and discardStderr(false) | discardStdout(false) and discardStderr(true) | discardStdout(true) and discardStderr(false) | |
|---|---|---|---|
| redirectStderr(true) and dumpOutput(true) |
System.out: STDOUT and STDERR interleaved System.err: unchanged |
System.out: STDOUT System.err: unchanged |
System.out: STDERR;
The command's STDERR will be written to the stream referenced by
System.err: unchanged |
| redirectStderr(false) and dumpOutput(true) |
System.out: STDOUT System.err: STDERR |
System.out: STDOUT System.err: unchanged |
System.out: unchanged System.err: STDERR |
The table below shows how different parameter combinations affect the
properties of CommandOutputControl.Result objects returned by
execute(ProcessBuilder, long) or
execute(ToolProvider, long, String...) when processing character
streams:
| saveOutput(true) | saveFirstLineOfOutput() | |
|---|---|---|
| redirectStderr(true) and discardStdout(false) and discardStderr(false) |
content(): STDOUT and STDERR interleaved
findStdout():
findStderr(): |
content(): a single-item list containing the first line of interleaved STDOUT and STDERR if the command produced any output; otherwise, an empty list
findStdout():
findStderr(): |
| redirectStderr(false) and discardStdout(false) and discardStderr(false) |
content(): STDOUT followed by STDERR stdout(): STDOUT stderr(): STDERR |
content(): a list containing at most two items: the first line of STDOUT (if the command produced any), followed by the first line of STDERR (if the command produced any) stdout(): The first line of STDOUT (if the command produced any); otherwise an empty list findStderr(): The first line of STDERR (if the command produced any); otherwise an empty list |
| redirectStderr(true) and discardStdout(false) and discardStderr(true) |
content(): STDOUT stdout(): The same as content()
findStderr(): |
content(): The first line of STDOUT (if the command produced any); otherwise an empty list stdout(): The same as content()
findStderr(): |
| redirectStderr(false) and discardStdout(false) and discardStderr(true) |
content(): STDOUT stdout(): The same as content() stderr(): an empty list |
content(): The first line of STDOUT (if the command produced any); otherwise an empty list stdout(): The same as content() stderr(): an empty list |
| redirectStderr(true) and discardStdout(true) and discardStderr(false) |
content(): STDERR stdout(): The same as content()
findStderr(): |
content(): The first line of STDERR (if the command produced any); otherwise an empty list stdout(): The same as content()
findStderr(): |
| redirectStderr(false) and discardStdout(true) and discardStderr(false) |
content(): STDERR findStdout(): an empty list stderr(): The same as content() |
content(): The first line of STDERR (if the command produced any); otherwise an empty list findStdout(): an empty list stderr(): The same as content() |
The table below shows how different parameter combinations affect the
properties of CommandOutputControl.Result objects returned by
execute(ProcessBuilder, long) or
execute(ToolProvider, long, String...) when processing byte streams:
| saveOutput(true) or saveFirstLineOfOutput() | |
|---|---|
| redirectStderr(true) and discardStdout(false) and discardStderr(false) |
byteContent(): STDOUT and STDERR interleaved
findByteStdout():
findByteStderr(): |
| redirectStderr(false) and discardStdout(false) and discardStderr(false) |
byteContent(): STDOUT followed by STDERR byteStdout(): STDOUT byteStderr(): STDERR |
| redirectStderr(true) and discardStdout(false) and discardStderr(true) |
byteContent(): STDOUT byteStdout(): The same as byteContent()
findByteStderr(): |
| redirectStderr(false) and discardStdout(false) and discardStderr(true) |
byteContent(): STDOUT byteStdout(): The same as byteContent() byteStderr(): an empty array |
| redirectStderr(true) and discardStdout(true) and discardStderr(false) |
byteContent(): STDERR byteStdout(): The same as byteContent()
findByteStderr(): |
| redirectStderr(false) and discardStdout(true) and discardStderr(false) |
byteContent(): STDERR findByteStdout(): an empty array byteStderr(): The same as byteContent() |
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic interfacestatic final recordstatic final recordstatic final recordstatic final classstatic class -
Field Summary
FieldsModifier and TypeFieldDescription -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbinaryOutput(boolean v) Specifies whether this object will treat output streams of subsequently executed commands as byte streams rather than character streams.charset()Returns the value passed in the last call ofcharset(Charset)method on this object, orStandardCharsets.UTF_8if the method has not been called.Sets character encoding that will be applied to the stdout and the stderr streams of commands (subprocesses andToolProvider-s) subsequently executed by this object.copy()Returns a deep copy of this object.createExecutable(ToolProvider tp, String... args) discardStderr(boolean v) Specifies whether stderr streams from commands subsequently executed by this object will be discarded.discardStdout(boolean v) Specifies whether stdout streams from commands subsequently executed by this object will be discarded.dumpOutput(boolean v) Specifies whether this object will dump the output streams from subsequently executed commands into the streams returned bydumpStdout()anddumpStdout()methods respectively.Returns the value passed in the last call ofdumpStderr(PrintStream)method on this object, orSystem.errif the method has not been called.Specifies the stream where stderr streams from commands subsequently executed by this object will be dumped.Returns the value passed in the last call ofdumpStdout(PrintStream)method on this object, orSystem.outif the method has not been called.Specifies the stream where stdout streams from commands subsequently executed by this object will be dumped.booleanReturns the value passed in the last call ofbinaryOutput(boolean)method on this object, orfalseif the method has not been called.booleanReturns the value passed in the last call ofdiscardStderr(boolean)method on this object, orfalseif the method has not been called.booleanReturns the value passed in the last call ofdiscardStdout(boolean)method on this object, orfalseif the method has not been called.booleanReturns the value passed in the last call ofdumpOutput(boolean)method on this object, orfalseif the method has not been called.booleanReturns the value passed in the last call ofredirectStderr(boolean)method on this object, orfalseif the method has not been called.booleanReturns whether this object will save the first line of the output of commands subsequently executed.booleanReturns whether this object will save the complete output of commands subsequently executed.booleanReturns the value passed in the last call ofstoreOutputInFiles(boolean)method on this object, orfalseif the method has not been called.Returns anOptionalwrapping the value passed in the last call ofprocessListener(Consumer)method on this object, or an emptyOptionalif the method has not been called ornullwas passed in the last call.Sets the callback to be invoked when this object starts a subprocess from subsequentexecute(ProcessBuilder, long)calls.redirectStderr(boolean v) Specifies whether the stderr stream will be redirected into the stdout stream for commands subsequently executed by this object.Specifies whether the first line of the output, combined from the stdout and stderr streams of commands subsequently executed by this object, will be saved.saveOutput(boolean v) Specifies whether the full output produced by commands subsequently executed by this object will be saved.storeOutputInFiles(boolean v) Specifies whether stderr and stdout streams for subprocesses subsequently executed by this object will be stored in files.
-
Field Details
-
EMPTY_EXECUTABLE_SPEC
-
-
Constructor Details
-
CommandOutputControl
public CommandOutputControl()
-
-
Method Details
-
saveOutput
Specifies whether the full output produced by commands subsequently executed by this object will be saved.If
vistrue, both stdout and stderr streams will be saved; otherwise, they will not be saved.This setting is mutually exclusive with
saveFirstLineOfOutput().- Parameters:
v-trueto save the full stdout and stderr streams;falseotherwise- Returns:
- this
-
isSaveOutput
public boolean isSaveOutput()Returns whether this object will save the complete output of commands subsequently executed.- Returns:
trueif this object will save the full output of commands it executes subsequently;falseotherwise
-
saveFirstLineOfOutput
Specifies whether the first line of the output, combined from the stdout and stderr streams of commands subsequently executed by this object, will be saved.This setting is mutually exclusive with
saveOutput(boolean).- Returns:
- this
-
isSaveFirstLineOfOutput
public boolean isSaveFirstLineOfOutput()Returns whether this object will save the first line of the output of commands subsequently executed.- Returns:
trueif this object will save the first line of the output of commands it executes subsequently;falseotherwise
-
dumpOutput
Specifies whether this object will dump the output streams from subsequently executed commands into the streams returned bydumpStdout()anddumpStdout()methods respectively.If this object is configured to redirect stderr of subsequently executed commands into their stdout (
redirectStderr(true)), their output streams will be dumped into the stream returned bydumpStdout()method. Otherwise, their stdout and stderr streams will be dumped into the stream returned bydumpStdout()anddumpStderr()methods respectively.- Parameters:
v- if output streams from subsequently executed commands will be dumped into streams returned bydumpStdout()anddumpStderr()methods respectively- Returns:
- this
- See Also:
-
isDumpOutput
public boolean isDumpOutput()Returns the value passed in the last call ofdumpOutput(boolean)method on this object, orfalseif the method has not been called.- Returns:
- the value passed in the last call of
dumpOutput(boolean)
-
binaryOutput
Specifies whether this object will treat output streams of subsequently executed commands as byte streams rather than character streams.- Parameters:
v-trueif this object will treat the output streams of subsequently executed commands as byte streams, andfalseotherwise- Returns:
- this
-
isBinaryOutput
public boolean isBinaryOutput()Returns the value passed in the last call ofbinaryOutput(boolean)method on this object, orfalseif the method has not been called.- Returns:
- the value passed in the last call of
binaryOutput(boolean)
-
charset
Sets character encoding that will be applied to the stdout and the stderr streams of commands (subprocesses andToolProvider-s) subsequently executed by this object. The default encoding isUTF-8.The value will be ignored if this object is configured for byte output streams.
- Parameters:
v- character encoding for output streams of subsequently executed commands- Returns:
- this
- See Also:
-
charset
Returns the value passed in the last call ofcharset(Charset)method on this object, orStandardCharsets.UTF_8if the method has not been called.- Returns:
- the character encoding that will be applied to the stdout and stderr streams of commands subsequently executed by this object
-
redirectStderr
Specifies whether the stderr stream will be redirected into the stdout stream for commands subsequently executed by this object.- Parameters:
v-trueif the stderr stream of commands subsequently executed by this object will be redirected into the stdout stream;falseotherwise- Returns:
- this
- See Also:
-
isRedirectStderr
public boolean isRedirectStderr()Returns the value passed in the last call ofredirectStderr(boolean)method on this object, orfalseif the method has not been called.- Returns:
- the value passed in the last call of
redirectStderr(boolean)
-
storeOutputInFiles
Specifies whether stderr and stdout streams for subprocesses subsequently executed by this object will be stored in files.By default, if an output stream of a subprocess is configured for saving, this object will retrieve the content using
Process.getInputStream()function for stdout andProcess.getErrorStream()function for stderr. However, these functions don't always work correctly due to a JDK-8236825 bug still reproducible on macOS JDK26. The alternative way to get the content of output streams of a subprocess is to redirect them into files and read these files when the subprocess terminates.It will use
Files.createTempFile("jpackageOutputTempFile", ".tmp")to create a file for each subprocess's output stream configured for saving. All created files will be automatically deleted at the exit ofexecute(ProcessBuilder, long)method.Doesn't apply to executing
ToolProvider-s.Storing output streams in files takes longer than managing them in memory and should be avoided if possible.
- Parameters:
v-trueif this object will use files to store saved output streams of subsequently executed subprocesses;falseotherwise- Returns:
- this
-
isStoreOutputInFiles
public boolean isStoreOutputInFiles()Returns the value passed in the last call ofstoreOutputInFiles(boolean)method on this object, orfalseif the method has not been called.- Returns:
- the value passed in the last call of
storeOutputInFiles(boolean)
-
discardStdout
Specifies whether stdout streams from commands subsequently executed by this object will be discarded.- Parameters:
v-trueif this object will discard stdout streams from commands subsequently executed by this object;falseotherwise- Returns:
- this
-
isDiscardStdout
public boolean isDiscardStdout()Returns the value passed in the last call ofdiscardStdout(boolean)method on this object, orfalseif the method has not been called.- Returns:
- the value passed in the last call of
discardStdout(boolean)
-
discardStderr
Specifies whether stderr streams from commands subsequently executed by this object will be discarded.- Parameters:
v-trueif this object will discard stderr streams from commands subsequently executed by this object;falseotherwise- Returns:
- this
-
isDiscardStderr
public boolean isDiscardStderr()Returns the value passed in the last call ofdiscardStderr(boolean)method on this object, orfalseif the method has not been called.- Returns:
- the value passed in the last call of
discardStderr(boolean)
-
dumpStdout
Specifies the stream where stdout streams from commands subsequently executed by this object will be dumped.If the
nullis specified and this object configuration is equivalent todumpOutput(true).saveOutput(false).discardStdout(false)the stdout streams from commands subsequently executed by this object will be written into the file descriptor associated with theSystsem.outstream. If you want them to be written into theSystsem.outobject, pass theSystsem.outreference into this function.- Parameters:
v- the stream where stdout streams from commands subsequently executed by this object will be dumped;nullpermitted- Returns:
- this
-
dumpStdout
Returns the value passed in the last call ofdumpStdout(PrintStream)method on this object, orSystem.outif the method has not been called.- Returns:
- the stream where stdout streams from commands subsequently executed by this object will be dumped
-
dumpStderr
Specifies the stream where stderr streams from commands subsequently executed by this object will be dumped.If the
nullis specified and this object configuration is equivalent todumpOutput(true).saveOutput(false).redirectStderr(false).discardStderr(false)the stderr streams from commands subsequently executed by this object will be written into the file descriptor associated with theSystsem.errstream. If you want them to be written into theSystsem.errobject, pass theSystsem.errreference into this function.- Parameters:
v- the stream where stderr streams from commands subsequently executed by this object will be dumped;nullpermitted- Returns:
- this
-
dumpStderr
Returns the value passed in the last call ofdumpStderr(PrintStream)method on this object, orSystem.errif the method has not been called.- Returns:
- the stream where stderr streams from commands subsequently executed by this object will be dumped
-
processListener
Sets the callback to be invoked when this object starts a subprocess from subsequentexecute(ProcessBuilder, long)calls.This object maintains a single callback. Calling this method replaces any previously set callback.
The callback is invoked on the thread that calls
execute(ProcessBuilder, long)after the subprocess's output streams begin being pumped.- Parameters:
v- the callback for notifying a subprocess being started ornull- Returns:
- this
-
processListener
Returns anOptionalwrapping the value passed in the last call ofprocessListener(Consumer)method on this object, or an emptyOptionalif the method has not been called ornullwas passed in the last call.- Returns:
- an
Optionalwrapping the value passed in the last call ofprocessListener(Consumer)
-
copy
Returns a deep copy of this object. Changes to the copy will not affect the original.- Returns:
- a deep copy of this object
-
createExecutable
-
createExecutable
-
description
-