Class Commandline
- All Implemented Interfaces:
- 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.
- Author:
- thomas.haas@softwired-inc.com, Stefan Bodewig
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic classclassClass to keep track of the position of an Argument.
- 
Field SummaryFields
- 
Constructor SummaryConstructorsConstructorDescriptionCreate a new command line object.Commandline(String toProcess) Create a new command line object, given a command following POSIX sh quoting rulesCommandline(String toProcess, Shell shell) Create a new command line object.Commandline(Shell shell) Create a new command line object.
- 
Method SummaryModifier and TypeMethodDescriptionvoidvoidAdds an argument object to our list of args.voidaddArguments(String[] line) voidaddEnvironment(String name, String value) Add an environment variablevoidAdd system environment variablesvoidclear()Clear out the whole command line.voidClear out the arguments but leave the executable in place for another operation.clone()Creates an argument object.createArg(boolean insertAtStart) Deprecated.UsecreateArg()insteadcreateArgument(boolean insertAtStart) Deprecated.UsecreateArg(boolean)insteadThis marker can be used to locate a position on the commandline - to insert something for example - when all parameters have been set.execute()Executes the command.String[]String[]String[]Return an executable name, quoted for shell use.longgetPid()String[]Returns the executable and all defined arguments.getShell()Get the shell to be used in this command line.String[]Returns the shell, executable and all defined arguments.static StringquoteArgument(String argument) Deprecated.UseCommandLineUtils.quote(String)instead.voidsetExecutable(String executable) Sets the executable to run.voidsetPid(long pid) voidAllows to set the shell to be used in this command line.voidsetWorkingDirectory(File workingDirectory) Sets execution directory.voidsetWorkingDirectory(String path) Sets execution directory.intsize()toString()static StringDeprecated.UseCommandLineUtils.toString(String[])instead.static String[]translateCommandline(String toProcess) Deprecated.UseCommandLineUtils.translateCommandline(String)instead.
- 
Field Details- 
OS_NAMEDeprecated.UseOsclass instead.- See Also:
 
- 
WINDOWSDeprecated.UseOsclass instead.- See Also:
 
- 
arguments
- 
envVars
- 
executableDeprecated.UsesetExecutable(String)instead.
 
- 
- 
Constructor Details- 
CommandlineCreate a new command line object. Shell is autodetected from operating system Shell usage is only desirable when generating code for remote execution.- Parameters:
- toProcess- sh to process
- shell- Shell to use
 
- 
CommandlineCreate a new command line object. Shell is autodetected from operating system Shell usage is only desirable when generating code for remote execution.- Parameters:
- shell- the Shell
 
- 
CommandlineCreate a new command line object, given a command following POSIX sh quoting rules- Parameters:
- toProcess- the process
 
- 
Commandlinepublic Commandline()Create a new command line object.
 
- 
- 
Method Details- 
getPidpublic long getPid()
- 
setPidpublic void setPid(long pid) 
- 
createArgumentDeprecated.UsecreateArg()insteadCreates an argument object. Each commandline object has at most one instance of the argument class. This method calls this.createArgument(false).- Returns:
- the argument object.
- See Also:
 
- 
createArgumentDeprecated.UsecreateArg(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. - Parameters:
- insertAtStart- if true, the argument is inserted at the beginning of the list of args, otherwise it is appended.
- Returns:
- Argument the argument Object
 
- 
createArgCreates an argument object. Each commandline object has at most one instance of the argument class. This method calls this.createArgument(false).- Returns:
- the argument object.
- See Also:
 
- 
createArg- Parameters:
- insertAtStart- if true, the argument is inserted at the beginning of the list of args, otherwise it is appended.
- Returns:
- Creates an argument object and adds it to our list of args.
 
 Each commandline object has at most one instance of the argument class. 
 
- 
addArg- Parameters:
- argument- the argument
- See Also:
 
- 
addArgAdds an argument object to our list of args.- Parameters:
- argument- the argument
- insertAtStart- if true, the argument is inserted at the beginning of the list of args, otherwise it is appended.
 
- 
setExecutableSets the executable to run.- Parameters:
- executable- the executable
 
- 
getLiteralExecutable- Returns:
- Executable to be run, as a literal string (no shell quoting/munging)
 
- 
getExecutableReturn an executable name, quoted for shell use. Shell usage is only desirable when generating code for remote execution.- Returns:
- Executable to be run, quoted for shell interpretation
 
- 
addArguments
- 
addEnvironmentAdd an environment variable- Parameters:
- name- name
- value- value
 
- 
addSystemEnvironmentAdd system environment variables- Throws:
- Exception- if error
 
- 
getEnvironmentVariables- Returns:
- String[] Return the list of environment variables
- Throws:
- CommandLineException- if error
 
- 
getCommandline- Returns:
- Returns the executable and all defined arguments.
      For Windows Family, getShellCommandline()is returned
 
- 
getRawCommandlineReturns the executable and all defined arguments.- Returns:
- the command line as array not escaped neither quoted
 
- 
getShellCommandlineReturns the shell, executable and all defined arguments. Shell usage is only desirable when generating code for remote execution.- Returns:
- the command line as array
 
- 
getArguments- Returns:
- Returns all arguments defined by addLine,addValueor the argument object.
 
- 
toString
- 
sizepublic int size()
- 
clone
- 
clearpublic void clear()Clear out the whole command line.
- 
clearArgspublic void clearArgs()Clear out the arguments but leave the executable in place for another operation.
- 
createMarkerThis marker can be used to locate a position on the commandline - to insert something for example - when all parameters have been set. - Returns:
- Return a marker.
 
- 
setWorkingDirectorySets execution directory.- Parameters:
- path- the working directory as String
 
- 
setWorkingDirectorySets execution directory.- Parameters:
- workingDirectory- the File used as working directory
 
- 
getWorkingDirectory
- 
executeExecutes the command.- Returns:
- the Process
- Throws:
- CommandLineException- if error
 
- 
getSystemEnvVars- Throws:
- Exception
 
- 
setShellAllows to set the shell to be used in this command line. Shell usage is only desirable when generating code for remote execution.- Parameters:
- shell- Shell to use
- Since:
- 1.2
 
- 
getShellGet the shell to be used in this command line. Shell usage is only desirable when generating code for remote execution.- Returns:
- the Shell
- Since:
- 1.2
 
- 
translateCommandlineDeprecated.UseCommandLineUtils.translateCommandline(String)instead.- Parameters:
- toProcess- the process
- Returns:
- the command line arguments
- Throws:
- Exception- if error happen
 
- 
quoteArgumentDeprecated.UseCommandLineUtils.quote(String)instead.- Parameters:
- argument- the argument
- Returns:
- the quote arg
- Throws:
- CommandLineException- if error happen
 
- 
toStringDeprecated.UseCommandLineUtils.toString(String[])instead.- Parameters:
- line- the lines
- Returns:
- lines as single String
 
 
- 
setExecutable(String)instead.