Java - Array Command-Line Arguments

Introduction

A Java application is run at the command line like so:

java <options-list> <classname>

You can also pass command-line arguments to a Java application by specifying arguments after the class name.

java <classname> <List of Command-line Arguments>

Each argument in the argument list is separated by a space.

For example, the following command runs the com.book2s.array.Test class and passes three names as the command-line arguments:

java com.book2s.array.Test Cat Dog Rat

Example

Processing Command-line Arguments Inside the main() Method

Demo

public class Main {
  public static void main(String[] args) {
    // args contains all command-line arguments
    System.out.println("Total Arguments:" + args.length);

    // Display all arguments
    for (int i = 0; i < args.length; i++) {
      System.out.println("Argument #" + (i + 1) + ":" + args[i]);
    }/* ww w  .j a  va 2  s . c  om*/
  }
}

Result

The following table shows the command to run the com.book2s.array.CommandLine class and the corresponding output.

Command
Output
java com.book2s.array.CommandLine
Total Arguments:0
java com.book2s.array.CommandLine Cat Dog Rat



Total Arguments:3
Argument #1:Cat
Argument #2:Dog
Argument #3:Rat
java com.book2s.array.CommandLine "Cat Dog Rat"

Total Arguments:1
Argument #1:Cat Dog Rat
java com.book2s.array.CommandLine 29 Dogs


Total Arguments:2
Argument #1:29
Argument #2:Dogs

The following code checks the command line parameters.

Demo

import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    System.out.println(Arrays.toString(args));

    // Make sure we received three arguments and the
    // the second argument has only one character to indicate operation.
    if (!(args.length == 3 && args[1].length() == 1)) {
      printUsage();/*from  w  w w . ja  va2  s. c o  m*/
      return; // Stop the program here
    }

    // Parse the two number operands. Place the parsing code inside a try-catch,
    // so we will handle the error in case both operands are not numbers.
    double n1 = 0.0;
    double n2 = 0.0;
    try {
      n1 = Double.parseDouble(args[0]);
      n2 = Double.parseDouble(args[2]);
    } catch (NumberFormatException e) {
      System.out.println("Both operands must be a number");
      printUsage();
      return; // Stop the program here
    }
    String operation = args[1];
    double result = compute(n1, n2, operation);

    // Print the result
    System.out.println(args[0] + args[1] + args[2] + "=" + result);
  }

  public static double compute(double n1, double n2, String operation) {
    // Initialize the result with not-a-number
    double result = Double.NaN;

    switch (operation) {
    case "+":
      result = n1 + n2;
      break;
    case "-":
      result = n1 - n2;
      break;
    case "*":
      result = n1 * n2;
      break;
    case "/":
      result = n1 / n2;
      break;
    default:
      System.out.println("Invalid operation:" + operation);
    }

    return result;
  }

  public static void printUsage() {
    System.out.println("Usage: java Main expr");
    System.out.println("Where expr could be:");
    System.out.println("n1 + n1");
    System.out.println("n1 - n2");
    System.out.println("n1 * n2");
    System.out.println("n1 / n2");
    System.out.println("n1 and n2 are two numbers");
  }
}

Result