public class Commandline extends Object implements 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 |
class |
Commandline.Marker
Class to keep track of the position of an Argument.
|
Modifier and Type | Field and Description |
---|---|
protected Vector<Arg> |
arguments |
protected Map<String,String> |
envVars |
protected String |
executable
Deprecated.
Use
setExecutable(String) instead. |
protected static String |
OS_NAME
Deprecated.
Use
Os class instead. |
protected static String |
WINDOWS
Deprecated.
Use
Os class instead. |
Constructor and Description |
---|
Commandline()
Create a new command line object.
|
Commandline(Shell shell)
Create a new command line object.
|
Commandline(String toProcess)
Create a new command line object, given a command following POSIX sh quoting rules
|
Commandline(String toProcess,
Shell shell)
Create a new command line object.
|
Modifier and Type | Method and Description |
---|---|
void |
addArg(Arg argument) |
void |
addArg(Arg argument,
boolean insertAtStart)
Adds an argument object to our list of args.
|
void |
addArguments(String[] line) |
void |
addEnvironment(String name,
String value)
Add an environment variable
|
void |
addSystemEnvironment()
Add system environment variables
|
void |
clear()
Clear out the whole command line.
|
void |
clearArgs()
Clear out the arguments but leave the executable in place for another operation.
|
Object |
clone() |
Arg |
createArg()
Creates an argument object.
|
Arg |
createArg(boolean insertAtStart) |
Commandline.Argument |
createArgument()
Deprecated.
Use
createArg() instead |
Commandline.Argument |
createArgument(boolean insertAtStart)
Deprecated.
Use
createArg(boolean) instead |
Commandline.Marker |
createMarker()
This marker can be used to locate a position on the commandline - to insert something for example - when all
parameters have been set.
|
Process |
execute()
Executes the command.
|
String[] |
getArguments() |
String[] |
getCommandline() |
String[] |
getEnvironmentVariables() |
String |
getExecutable()
Return an executable name, quoted for shell use.
|
String |
getLiteralExecutable() |
long |
getPid() |
String[] |
getRawCommandline()
Returns the executable and all defined arguments.
|
Shell |
getShell()
Get the shell to be used in this command line.
|
String[] |
getShellCommandline()
Returns the shell, executable and all defined arguments.
|
Properties |
getSystemEnvVars() |
File |
getWorkingDirectory() |
static String |
quoteArgument(String argument)
Deprecated.
Use
CommandLineUtils.quote(String) instead. |
void |
setExecutable(String executable)
Sets the executable to run.
|
void |
setPid(long pid) |
void |
setShell(Shell shell)
Allows to set the shell to be used in this command line.
|
void |
setWorkingDirectory(File workingDirectory)
Sets execution directory.
|
void |
setWorkingDirectory(String path)
Sets execution directory.
|
int |
size() |
String |
toString() |
static String |
toString(String[] line)
Deprecated.
Use
CommandLineUtils.toString(String[]) instead. |
static String[] |
translateCommandline(String toProcess)
Deprecated.
Use
CommandLineUtils.translateCommandline(String) instead. |
@Deprecated protected static final String OS_NAME
Os
class instead.@Deprecated protected static final String WINDOWS
Os
class instead.@Deprecated protected String executable
setExecutable(String)
instead.public Commandline(String toProcess, Shell shell)
toProcess
- sh to processshell
- Shell to usepublic Commandline(Shell shell)
shell
- the Shellpublic Commandline(String toProcess)
toProcess
- the processpublic Commandline()
public long getPid()
public void setPid(long pid)
@Deprecated public Commandline.Argument createArgument()
createArg()
insteadCreates an argument object.
Each commandline object has at most one instance of the argument class. This method calls
this.createArgument(false)
.
createArgument(boolean)
@Deprecated public Commandline.Argument createArgument(boolean insertAtStart)
createArg(boolean)
insteadCreates an argument object and adds it to our list of args.
Each commandline object has at most one instance of the argument class.
insertAtStart
- if true, the argument is inserted at the beginning of the list of args, otherwise it is
appended.public Arg createArg()
Creates an argument object.
Each commandline object has at most one instance of the argument class. This method calls
this.createArgument(false)
.
createArgument(boolean)
public Arg createArg(boolean insertAtStart)
insertAtStart
- if true, the argument is inserted at the beginning of the list of args, otherwise it is
appended.Each commandline object has at most one instance of the argument class.
public void addArg(Arg argument)
argument
- the argumentaddArg(Arg,boolean)
public void addArg(Arg argument, boolean insertAtStart)
argument
- the argumentinsertAtStart
- if true, the argument is inserted at the beginning of the list of args, otherwise it is
appended.public void setExecutable(String executable)
executable
- the executablepublic String getLiteralExecutable()
public String getExecutable()
public void addArguments(String[] line)
public void addEnvironment(String name, String value)
name
- namevalue
- valuepublic void addSystemEnvironment() throws Exception
Exception
- if errorpublic String[] getEnvironmentVariables() throws CommandLineException
CommandLineException
- if errorpublic String[] getCommandline()
getShellCommandline()
is returnedpublic String[] getRawCommandline()
public String[] getShellCommandline()
public String[] getArguments()
addLine
, addValue
or the argument object.public int size()
public void clear()
public void clearArgs()
public Commandline.Marker createMarker()
This marker can be used to locate a position on the commandline - to insert something for example - when all parameters have been set.
public void setWorkingDirectory(String path)
path
- the working directory as Stringpublic void setWorkingDirectory(File workingDirectory)
workingDirectory
- the File used as working directorypublic File getWorkingDirectory()
public Process execute() throws CommandLineException
CommandLineException
- if errorpublic Properties getSystemEnvVars() throws Exception
Exception
public void setShell(Shell shell)
shell
- Shell to usepublic Shell getShell()
@Deprecated public static String[] translateCommandline(String toProcess) throws Exception
CommandLineUtils.translateCommandline(String)
instead.toProcess
- the processException
- if error happen@Deprecated public static String quoteArgument(String argument) throws CommandLineException
CommandLineUtils.quote(String)
instead.argument
- the argumentCommandLineException
- if error happen@Deprecated public static String toString(String[] line)
CommandLineUtils.toString(String[])
instead.line
- the linesCopyright © 2001–2022 The Apache Software Foundation. All rights reserved.