24

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

burntsugar
  • 57,360
  • 21
  • 58
  • 81
  • Possible duplicate of [How to parse command line arguments in Java?](http://stackoverflow.com/questions/367706/how-to-parse-command-line-arguments-in-java) – xenteros Aug 30 '16 at 06:57

9 Answers9

41

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)
Michael Myers
  • 188,989
  • 46
  • 291
  • 292
John Ellinwood
  • 14,291
  • 7
  • 38
  • 48
38
public class YourClass {
    public static void main(String[] args) {
        if (args.length > 0 && args[0].equals("a")){
            //...
        }
    }
}
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Björn
  • 29,019
  • 9
  • 65
  • 81
19

Every Java program starts with

public static void main(String[] args) {

That array of type String that main() takes as a parameter holds the command line arguments to your program. If the user runs your program as

$ java myProgram a

then args[0] will hold the String "a".

user456584
  • 86,427
  • 15
  • 75
  • 107
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
5

Command-line arguments are passed in the first String[] parameter to main(), e.g.

public static void main( String[] args ) {
}

In the example above, args contains all the command-line arguments.

The short, sweet answer to the question posed is:

public static void main( String[] args ) {
    if( args.length > 0 && args[0].equals( "a" ) ) {
        // first argument is "a"
    } else {
        // oh noes!?
    }
}
Rob
  • 47,999
  • 5
  • 74
  • 91
2

Command line arguments are accessible via String[] args parameter of main method.

For first argument you can check args[0]

entire code would look like

public static void main(String[] args) {
    if ("a".equals(args[0])) {
         // do something
    }
}
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
1

Try to pass value a and compare using the equals method like this:

public static void main(String str[]) {

    boolean b = str[0].equals("a");

    System.out.println(b);

}

Follow this link to know more about Command line argument in Java

Samoth
  • 1,691
  • 17
  • 24
1

Your main method has a String[] argument. That contain the arguments that have been passed to your applications (it's often called args, but that's not a requirement).

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
0

Command line arguments are stored as strings in the String array String[] args that is passed tomain()`.

java [program name] [arg1,arg2 ,..]

Command line arguments are the inputs that accept from the command prompt while running the program. The arguments passed can be anything. Which is stored in the args[] array.

//Display all command line information
         class ArgDemo{
            public static void main(String args[]){
            System.out.println("there are "+args.length+"command-line arguments.");
            for(int i=0;i<args.length;i++)
            System.out.println("args["+i+"]:"+args[i]);
    }
    }

Example:

java Argdemo one two

The output will be:

there are 2 command line arguments:
they are:
arg[0]:one
arg[1]:two
Prudhvi Bellam
  • 71
  • 2
  • 12
  • In your example, what does the first line `javastack.java.` mean? I don't know for sure what the `It` in `It's used to accept input from the command prompt while running the program.` is referring to, but the sequence is 1) parse command 2) setup parameters 3) run program. I don't know for sure what the `This` in `This essentially speeds up the program execution when the program depends on user input.` is referring to, but there is nothing in _parameter passing_ to _speed up the program execution_. – greybeard Dec 18 '16 at 18:28
0

As everyone else has said... the .equals method is what you need.

In the off chance you used something like:

if(argv[0] == "a")

then it does not work because == compares the location of the two objects (physical equality) rather than the contents (logical equality).

Since "a" from the command line and "a" in the source for your program are allocated in two different places the == cannot be used. You have to use the equals method which will check to see that both strings have the same characters.

Another note... "a" == "a" will work in many cases, because Strings are special in Java, but 99.99999999999999% of the time you want to use .equals.

TofuBeer
  • 60,850
  • 18
  • 118
  • 163