public class Commandline
extends java.lang.Object
implements java.lang.Cloneable
Commandline objects help handling command lines specifying processes to execute.
The class can be used to define a command line as nested elements or as a helper to define a command line by an application.
<someelement>
<acommandline executable="/executable/to/run">
<argument value="argument 1" />
<argument line="argument_1 argument_2 argument_3" />
<argument value="argument 4" />
</acommandline>
</someelement>
The element someelement must provide a method
createAcommandline which returns an instance of this class.
| Modifier and Type | Class and Description |
|---|---|
static class |
Commandline.Argument
A single command line argument
|
| Modifier and Type | Field and Description |
|---|---|
private java.util.List<Arg> |
arguments |
private java.util.Map<java.lang.String,java.lang.String> |
envVars |
private Shell |
shell |
| Constructor and Description |
|---|
Commandline()
Create a new command line object.
|
Commandline(Shell shell)
Create a new command line object.
|
Commandline(java.lang.String toProcess)
Create a new command line object.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addArguments(java.lang.String... line) |
void |
addEnvironment(java.lang.String name,
java.lang.String value)
Add an environment variable.
|
void |
addSystemEnvironment()
Add system environment variables.
|
void |
clearArgs()
Clear out the arguments but leave the executable in place for another operation.
|
java.lang.Object |
clone() |
Arg |
createArg()
Creates an empty argument object and inserts it at the end of the argument list.
|
Arg |
createArg(boolean insertAtStart)
Creates an argument object and adds it to the list of args.
|
java.lang.Process |
execute()
Execute the command.
|
java.lang.String[] |
getArguments()
Returns all arguments defined by
addLine,
addValue or the argument object. |
java.lang.String[] |
getArguments(boolean mask)
Returns all arguments defined by
addLine,
addValue, or the argument object. |
java.lang.String[] |
getCommandline()
Returns the executable and all defined arguments.
|
java.lang.String[] |
getEnvironmentVariables()
Return the list of environment variables.
|
java.lang.String |
getExecutable() |
Shell |
getShell()
Get the shell to be used in this command line.
|
private java.lang.String[] |
getShellCommandline() |
private java.lang.String[] |
getShellCommandline(boolean mask) |
java.io.File |
getWorkingDirectory() |
private void |
setDefaultShell()
Sets the shell or command-line interpreter for the detected operating system,
and the shell arguments.
|
void |
setExecutable(java.lang.String executable)
Sets the executable to run.
|
(package private) void |
setShell(Shell shell)
Set the shell to be used for this command line.
|
void |
setWorkingDirectory(java.io.File workingDirectory)
Sets working directory.
|
void |
setWorkingDirectory(java.lang.String path)
Sets working directory.
|
java.lang.String |
toString() |
private final java.util.List<Arg> arguments
private final java.util.Map<java.lang.String,java.lang.String> envVars
private Shell shell
public Commandline(Shell shell)
shell - the shell instancepublic Commandline(java.lang.String toProcess)
throws CommandLineException
toProcess - the command to processCommandLineException - in case of unbalanced quotes.public Commandline()
private void setDefaultShell()
Sets the shell or command-line interpreter for the detected operating system, and the shell arguments.
public Arg createArg()
public Arg createArg(boolean insertAtStart)
insertAtStart - if true, the argument is inserted at the
beginning of the list of args. Otherwise it is appended.public void setExecutable(java.lang.String executable)
executable - the executablepublic java.lang.String getExecutable()
public void addArguments(java.lang.String... line)
line - the argumentspublic void addEnvironment(java.lang.String name,
java.lang.String value)
name - the name of the environment variablevalue - the appropriate valuepublic void addSystemEnvironment()
public java.lang.String[] getEnvironmentVariables()
public java.lang.String[] getCommandline()
private java.lang.String[] getShellCommandline()
private java.lang.String[] getShellCommandline(boolean mask)
mask - flag to mask any arguments (having his mask field to true)mask parameter is onpublic java.lang.String[] getArguments()
addLine,
addValue or the argument object.public java.lang.String[] getArguments(boolean mask)
addLine,
addValue, or the argument object.mask - flag to mask any arguments (having his mask field to true)public java.lang.String toString()
toString in class java.lang.Objectpublic java.lang.Object clone()
clone in class java.lang.Objectpublic void setWorkingDirectory(java.lang.String path)
path - the working directorypublic void setWorkingDirectory(java.io.File workingDirectory)
workingDirectory - the working directorypublic java.io.File getWorkingDirectory()
public void clearArgs()
public java.lang.Process execute()
throws CommandLineException
CommandLineException - in case of errorsvoid setShell(Shell shell)
shell - the shellpublic Shell getShell()