Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Command line arguments

Tags:

I am trying to detect whether the 'a' was entered as the first string argument.

like image 856
burntsugar Avatar asked Apr 04 '09 00:04

burntsugar


People also ask

Can Java take command line arguments?

A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched. When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of String s.


2 Answers

Use the apache commons cli if you plan on extending that past a single arg.

"The Apache Commons CLI library provides an API for parsing command line options passed to programs. It's also able to print help messages detailing the options available for a command line tool."

Commons CLI supports different types of options:

  • POSIX like options (ie. tar -zxvf foo.tar.gz)
  • GNU like long options (ie. du --human-readable --max-depth=1)
  • Java like properties (ie. java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo)
  • Short options with value attached (ie. gcc -O2 foo.c)
  • long options with single hyphen (ie. ant -projecthelp)
like image 143
John Ellinwood Avatar answered Oct 02 '22 17:10

John Ellinwood


public class YourClass {     public static void main(String[] args) {         if (args.length > 0 && args[0].equals("a")){             //...         }     } } 
like image 24
Björn Avatar answered Oct 02 '22 17:10

Björn