public class JavaCommandLine extends Object
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
Note that this class offers the subclasses to ehance the parsing process by overriding the processClassArgument(String, String)
method. To be able to achieve that, the evaluation of the commandline arguments needs to happen lazily.
See the parseCommandLine() method for subclassing guidelines.
This class is NOT thread-safe.
| Modifier and Type | Class and Description |
|---|---|
static class |
JavaCommandLine.OptionValueDelimiter
When parsing command line options, specifies the valid option value delimiter(s).
|
| Constructor and Description |
|---|
JavaCommandLine(String... args)
Same as
JavaCommandLine(args, false, OptionFormat.POSIX, OptionFormat.POSIX) |
JavaCommandLine(String[] args,
boolean includeSystemPropertiesFromClassArguments)
Same as
JavaCommandLine(args, includeSystemPropertiesFromClassArguments, OptionFormat.POSIX, OptionFormat.POSIX) |
JavaCommandLine(String[] args,
boolean includeSystemPropertiesFromClassArguments,
Set<JavaCommandLine.OptionValueDelimiter> shortClassOptionValueDelims,
Set<JavaCommandLine.OptionValueDelimiter> longClassOptionValueDelims) |
| Modifier and Type | Method and Description |
|---|---|
List<String> |
getArguments() |
List<String> |
getClassArguments() |
String |
getClassOption(CommandLineOption option) |
String |
getClassOption(CommandLineOption option,
String defaultValue) |
List<String> |
getClassPath() |
File |
getExecutableJarFile() |
File |
getJavaExecutable() |
List<String> |
getJavaOptions() |
String |
getMainClassName() |
Map<String,String> |
getSystemProperties() |
protected boolean |
isArgumentsParsed() |
boolean |
isClassOptionPresent(CommandLineOption option) |
protected void |
parseCommandLine()
This method can be called to process the command line from the arguments passed in the constructor.
|
protected void |
processClassArgument(String classArg,
String nextArg)
Override this method to do additional processing of the class arguments.
|
String |
toString() |
public JavaCommandLine(String... args)
JavaCommandLine(args, false, OptionFormat.POSIX, OptionFormat.POSIX)public JavaCommandLine(String[] args, boolean includeSystemPropertiesFromClassArguments)
JavaCommandLine(args, includeSystemPropertiesFromClassArguments, OptionFormat.POSIX, OptionFormat.POSIX)public JavaCommandLine(String[] args, boolean includeSystemPropertiesFromClassArguments, Set<JavaCommandLine.OptionValueDelimiter> shortClassOptionValueDelims, Set<JavaCommandLine.OptionValueDelimiter> longClassOptionValueDelims)
protected void parseCommandLine()
Any class overriding the processClassArgument(String, String) method should make sure to call this method
if it finds that the data extracted in that method is still uninitialized.
Typically, this will happen during a getter for such data:
public Data getDataExtractedFromCommandLine() {
if (data == null) {
parseCommandLine();
}
return data;
}
The data variable would then be initialized as part of the processClassArgument(String, String) method
that gets called during the execution of this method.
Alternatively to the null check on the data, the subclass can use the isArgumentsParsed() method that
returns true only if this method successfully finished.
If you are overriding this method make sure to call super.parseCommandLine() before any of your other
logic, otherwise you may end up with an endless loop (and eventually stack overflow) if you try to access
any of the getters of the data extracted from the commandline (like getClassArguments(),
getClassPath(), etc).
protected boolean isArgumentsParsed()
parseCommandLine() method was called and successfully finished.protected void processClassArgument(String classArg, String nextArg)
parseCommandLine() call but after all other properties are processed.
It is therefore safe to call getClassArguments(), getExecutableJarFile() and all other getters
defined by JavaCommandLine. At the time this method is called during parseCommandLine(), the
default implementation of isArgumentsParsed() already returns true.
This method is called at a stage during the parsing of the commandline where all the properties are still writeable
- you can modify the system properties and other collections.
By default this method does nothing.
classArg - nextArg - @NotNull public File getJavaExecutable()
@Nullable public String getMainClassName()
@Nullable public File getExecutableJarFile()
@Nullable public String getClassOption(CommandLineOption option)
option - the class option to look for@Nullable public String getClassOption(CommandLineOption option, String defaultValue)
option - the class option to look fordefaultValue - the value to return if the specified class option is not on the command linepublic boolean isClassOptionPresent(CommandLineOption option)
Copyright © 2008-2014 Red Hat, Inc.. All Rights Reserved.