public class ProcBuilder extends Object
Similar to ProcessBuilder
but with additional features.
Modifier and Type | Class and Description |
---|---|
static class |
ProcBuilder.Matcher
Specifies interface for defining OS-specific commands.
|
Modifier and Type | Field and Description |
---|---|
static ProcBuilder.Matcher |
ANY
Operating system matcher: Any operating system.
|
static ProcBuilder.Matcher |
MAC
Operating system matcher: Any Mac system.
|
static ProcBuilder.Matcher |
UNIX
Operating system matcher: Any Unix or Linux system.
|
static ProcBuilder.Matcher |
WINDOWS
Operating system matcher: Any Windows system.
|
Constructor and Description |
---|
ProcBuilder() |
Modifier and Type | Method and Description |
---|---|
ProcBuilder |
append(Object... args)
Append to the command arguments.
|
ProcBuilder |
appendIf(ProcBuilder.Matcher m,
Object... args)
Append to the command arguments if the specified matcher matches.
|
ProcBuilder |
byLines()
Use by-lines mode.
|
ProcBuilder |
command(Object... args)
Command arguments.
|
ProcBuilder |
commandIf(ProcBuilder.Matcher m,
Object... args)
Command arguments if the specified matcher matches.
|
static ProcBuilder |
create()
Creates an empty process builder.
|
static ProcBuilder |
create(Object... args)
Creates a process builder with the specified arguments.
|
ProcBuilder |
directory(File directory)
Sets the directory where the command will be executed.
|
ProcBuilder |
env(Map env)
Append the specified environment variables to the process.
|
ProcBuilder |
environment(String key,
String val)
Append the specified environment variable.
|
String |
getOutput()
Run this command and returns the output as a simple string.
|
Scanner |
getScanner()
Returns the output from this process as a
Scanner . |
ProcBuilder |
logTo(Level level,
Logger logger)
Pipe output to the specified writer, including the command and return code.
|
ProcBuilder |
logTo(Writer w)
Pipe output to the specified writer, including the command and return code.
|
ProcBuilder |
logTo(Writer w,
boolean close)
Pipe output to the specified writer, including the command and return code.
|
ProcBuilder |
lp(IOPipe.LineProcessor lp)
Line processor to use to process/convert lines of output returned by the process.
|
ProcBuilder |
maxExitStatus(int maxExitStatus)
Sets the maximum allowed return code on the process call.
|
ProcBuilder |
merge()
Merge STDOUT and STDERR into a single stream.
|
ProcBuilder |
pipeTo(Writer w)
Pipe output to the specified writer, but don't close the writer.
|
ProcBuilder |
pipeTo(Writer w,
boolean close)
Pipe output to the specified writer.
|
int |
run()
Run this command and pipes the output to the specified writer or output stream.
|
public static final ProcBuilder.Matcher ANY
public static final ProcBuilder.Matcher WINDOWS
public static final ProcBuilder.Matcher MAC
public static final ProcBuilder.Matcher UNIX
public ProcBuilder()
public static ProcBuilder create(Object... args)
Equivalent to calling
args
- The command-line arguments.public static ProcBuilder create()
public ProcBuilder command(Object... args)
Arguments can be collections or arrays and will be automatically expanded.
args
- The command-line arguments.public ProcBuilder commandIf(ProcBuilder.Matcher m, Object... args)
Can be used for specifying OS-specific commands.
ProcBuilder pb = ProcBuilder
.create()
.commandIf(
m
- The matcher.args
- The command line arguments if matcher matches.public ProcBuilder append(Object... args)
Arguments can be collections or arrays and will be automatically expanded.
args
- The command-line arguments.public ProcBuilder appendIf(ProcBuilder.Matcher m, Object... args)
Arguments can be collections or arrays and will be automatically expanded.
m
- The matcher.args
- The command line arguments if matcher matches.public ProcBuilder merge()
public ProcBuilder byLines()
Flushes output after every line of input.
public ProcBuilder pipeTo(Writer w, boolean close)
The method can be called multiple times to write to multiple writers.
w
- The writer to pipe to.close
- Close the writer afterwards.public ProcBuilder pipeTo(Writer w)
w
- The writer to pipe to.public ProcBuilder logTo(Writer w, boolean close)
The method can be called multiple times to write to multiple writers.
w
- The writer to pipe to.close
- Close the writer afterwards.public ProcBuilder logTo(Writer w)
The method can be called multiple times to write to multiple writers. Don't close the writer afterwards.
w
- The writer to pipe to.public ProcBuilder logTo(Level level, Logger logger)
level
- The log level.logger
- The logger to log to.public ProcBuilder lp(IOPipe.LineProcessor lp)
lp
- The new line processor.public ProcBuilder env(Map env)
env
- The new set of environment variables.public ProcBuilder environment(String key, String val)
key
- The environment variable name.val
- The environment variable value.public ProcBuilder directory(File directory)
directory
- The directory.public ProcBuilder maxExitStatus(int maxExitStatus)
If the return code exceeds this value, an IOException is returned on the run()
command.
The default value is '0'.
maxExitStatus
- The maximum exit status.public int run() throws IOException, InterruptedException
IOException
- Thrown by underlying stream.InterruptedException
- Thread was interrupted.public String getOutput() throws IOException, InterruptedException
IOException
- Thrown by underlying stream.InterruptedException
- Thread was interrupted.public Scanner getScanner() throws IOException, InterruptedException
Scanner
.IOException
- Thrown by underlying stream.InterruptedException
- Thread was interrupted.Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.