public abstract class CommandLineUtils
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
CommandLineUtils.StringStreamConsumer
A
StreamConsumer providing consumed lines as a String . |
Modifier and Type | Field and Description |
---|---|
private static long |
MILLIS_PER_SECOND
Number of milliseconds per second.
|
private static long |
NANOS_PER_SECOND
Number of nanoseconds per second.
|
Constructor and Description |
---|
CommandLineUtils() |
Modifier and Type | Method and Description |
---|---|
static int |
executeCommandLine(Commandline cl,
java.io.InputStream systemIn,
StreamConsumer systemOut,
StreamConsumer systemErr) |
static int |
executeCommandLine(Commandline cl,
java.io.InputStream systemIn,
StreamConsumer systemOut,
StreamConsumer systemErr,
int timeoutInSeconds) |
static int |
executeCommandLine(Commandline cl,
StreamConsumer systemOut,
StreamConsumer systemErr) |
static int |
executeCommandLine(Commandline cl,
StreamConsumer systemOut,
StreamConsumer systemErr,
int timeoutInSeconds) |
static CommandLineCallable |
executeCommandLineAsCallable(Commandline cl,
java.io.InputStream systemIn,
StreamConsumer systemOut,
StreamConsumer systemErr,
int timeoutInSeconds)
Immediately forks a process, returns a callable that will block until process is complete.
|
static java.util.Properties |
getSystemEnvVars()
Gets the shell environment variables for this process.
|
static java.util.Properties |
getSystemEnvVars(boolean caseSensitive)
Return the shell environment variables.
|
private static void |
handleException(StreamFeeder streamFeeder,
java.lang.String streamName) |
private static void |
handleException(StreamPumper streamPumper,
java.lang.String streamName) |
static boolean |
isAlive(java.lang.Process p) |
static java.lang.String |
quote(java.lang.String argument)
|
static java.lang.String |
quote(java.lang.String argument,
boolean wrapExistingQuotes)
|
static java.lang.String |
quote(java.lang.String argument,
boolean escapeSingleQuotes,
boolean escapeDoubleQuotes,
boolean wrapExistingQuotes)
|
static java.lang.String |
toString(java.lang.String[] line) |
static java.lang.String[] |
translateCommandline(java.lang.String toProcess) |
private static final long MILLIS_PER_SECOND
private static final long NANOS_PER_SECOND
public static int executeCommandLine(Commandline cl, StreamConsumer systemOut, StreamConsumer systemErr) throws CommandLineException
CommandLineException
public static int executeCommandLine(Commandline cl, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds) throws CommandLineException
CommandLineException
public static int executeCommandLine(Commandline cl, java.io.InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr) throws CommandLineException
CommandLineException
public static int executeCommandLine(Commandline cl, java.io.InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds) throws CommandLineException
cl
- The command line to executesystemIn
- The input to read from, must be thread safesystemOut
- A consumer that receives output, must be thread safesystemErr
- A consumer that receives system error stream output, must be thread safetimeoutInSeconds
- Positive integer to specify timeout, zero and negative integers for no timeout.Process.exitValue()
CommandLineException
- or CommandLineTimeOutException if time out occurspublic static CommandLineCallable executeCommandLineAsCallable(Commandline cl, java.io.InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds) throws CommandLineException
cl
- The command line to executesystemIn
- The input to read from, must be thread safesystemOut
- A consumer that receives output, must be thread safesystemErr
- A consumer that receives system error stream output, must be thread safetimeoutInSeconds
- Positive integer to specify timeout, zero and negative integers for no timeout.Process.exitValue()
. "call"
must be called on this to be sure the forked process has terminated, no guarantees is made about any
internal state before after the completion of the call statementsCommandLineException
- or CommandLineTimeOutException if time out occursprivate static void handleException(StreamPumper streamPumper, java.lang.String streamName) throws CommandLineException
CommandLineException
private static void handleException(StreamFeeder streamFeeder, java.lang.String streamName) throws CommandLineException
CommandLineException
public static java.util.Properties getSystemEnvVars() throws java.io.IOException
getSystemEnvVars().get("path")
and getSystemEnvVars().get("PATH")
will in general return different values. However, on platforms
with case-insensitive environment variables like Windows, all variable names will be normalized to upper case.null
.java.io.IOException
- If the environment variables could not be queried from the shell.System.getenv() API, new in JDK 5.0, to get the same result since 2.0.2 System#getenv()
will be used if available in the current running jvm.
public static java.util.Properties getSystemEnvVars(boolean caseSensitive) throws java.io.IOException
caseSensitive == true
, then envar keys will all be
upper-case.caseSensitive
- Whether environment variable keys should be treated case-sensitively.java.io.IOException
- .System.getenv() API, new in JDK 5.0, to get the same result since 2.0.2 System#getenv()
will be used if available in the current running jvm.
public static boolean isAlive(java.lang.Process p)
public static java.lang.String[] translateCommandline(java.lang.String toProcess) throws java.lang.Exception
java.lang.Exception
public static java.lang.String quote(java.lang.String argument) throws CommandLineException
StringUtils.quoteAndEscape(String, char, char[], char[], char, boolean)
,
StringUtils.quoteAndEscape(String, char, char[], char, boolean)
, or
StringUtils.quoteAndEscape(String, char)
instead.Put quotes around the given String if necessary.
If the argument doesn't include spaces or quotes, return it as is. If it contains double quotes, use single quotes - else surround the argument by double quotes.
CommandLineException
- if the argument contains both, single and double quotes.public static java.lang.String quote(java.lang.String argument, boolean wrapExistingQuotes) throws CommandLineException
StringUtils.quoteAndEscape(String, char, char[], char[], char, boolean)
,
StringUtils.quoteAndEscape(String, char, char[], char, boolean)
, or
StringUtils.quoteAndEscape(String, char)
instead.Put quotes around the given String if necessary.
If the argument doesn't include spaces or quotes, return it as is. If it contains double quotes, use single quotes - else surround the argument by double quotes.
CommandLineException
- if the argument contains both, single and double quotes.public static java.lang.String quote(java.lang.String argument, boolean escapeSingleQuotes, boolean escapeDoubleQuotes, boolean wrapExistingQuotes) throws CommandLineException
StringUtils.quoteAndEscape(String, char, char[], char[], char, boolean)
,
StringUtils.quoteAndEscape(String, char, char[], char, boolean)
, or
StringUtils.quoteAndEscape(String, char)
instead.CommandLineException
public static java.lang.String toString(java.lang.String[] line)