Using Command-Line Arguments with main method

A command-line argument is the information that follows the program's name on the command line.

The command-line arguments are stored as string array passed to main().
For example, the following program displays all of the command-line arguments:

public class Main {
  public static void main(String args[]) {
    for (int i = 0; i < args.length; i++)
      System.out.println("args[" + i + "]: " + args[i]);
  }
}

Try executing this program, as shown here:


java Main this is a test 100

When you do, you will see the following output:


args[0]: this 
args[1]: is 
args[2]: a 
args[3]: test 
args[4]: 100
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.