A simple example of how to use args4j to add command line args to a simple “Hello World” type java application.
For example below command line execution would print “Hello arg4j!” instead of the default “Hello World!” if you don’t pass any args.
$ java -jar helloWorldParamaterized --msg='Hello arg4j!'
import org.kohsuke.args4j.CmdLineParser; | |
import org.kohsuke.args4j.Option; | |
/** | |
* Hello world! class that is paramaterized (with defaults) using arg4j. | |
* Example cli usage: java -jar helloWorldParamaterized –msg='Hello arg4j!' | |
*/ | |
public class helloWorldParamaterized | |
{ | |
@Option(name="–msg") | |
private String msg = "Hello World!"; | |
public static void main(String[] args) throws Exception { | |
new helloWorldParamaterized().doMain(args); | |
} | |
public void doMain(String[] args) throws Exception { | |
// make a parser | |
CmdLineParser parser = new CmdLineParser(this); | |
// parse args | |
parser.parseArgument(args); | |
// print message passed in from args | |
System.out.println(msg); | |
} | |
} |