Command Line Arguments in Java

Java command-line argument is an argument that is passed at the time of running the Java program. Command-line arguments directly follows the program's name on the command line when it is executed. We can use these command-line arguments as input in our Java program.

  • We need to pass the arguments as space-separated values.

  • We can pass primitive data types(like int, char, double, float etc) as well as strings as command line arguments. However, JVM internally converts each command line argument into string and pass it to main() method of the program as an array of strings named args(String[] args).

  • JVM stores the first command-line argument at args[0], the second at args[1], third at args[2], and so on.

  • We can check the number of arguments passed to a program by checking the length of args using args.length.

Java program of command line argument

public class CommandLineArguments {
    public static void main(String[] args) {
        int index;
        // Iterate through all CLI arguments and print.
        for(index = 0; index < args.length; index++) {
            System.out.println("args[" + index + "]: "
               + args[index]);
        }
    }
}

Here are the steps to run above java program in the command line by passing command line arguments.

  1. Save above program as CommandLineArguments.java
  2. Open the command prompt window and compile the program by running following command
    javac CommandLineArguments.java
  3. Now suppose you want to pass three string arguments("Tech","Crash" and "Course") while running this program then you can pass the arguments after the class name. Execute your program by passing space separated command line arguments as follows:
    java CommandLineArguments Tech Crash Course
  4. You will see following output in your command prompt.
    args[0]: Tech
    args[1]: Crash
    args[2]: Course

Passing numeric command line arguments

We know that the data of command-line arguments passed to main() method are always in the form of String. We can use string parser methods to convert string to corresponding primitive data type like int, float, double etc.

public class NumberCommandLineArguments {
    public static void main(String[] args) {
        int index;
        // Iterate through all CLI arguments and print.
        for(index = 0; index < args.length; index++) {
            System.out.println("args[" + index + "]: "
                    + Integer.parseInt(args[index]));
        }
    }
}
Output
javac NumberCommandLineArguments.java
java NumberCommandLineArguments 10 20 30
args[0]: 10
args[1]: 20
args[2]: 30

In above program, we used parseInt() method of Integer class to converts the string argument into an integer value. Similarly, we can use parseFloat() and parseDouble() method to convert a string argument into float and double respectively.


Important Points About Command-line Arguments
  • The command-line arguments passed to main method are in the form of String.
  • There is no limitation on the number of command-line arguments. We can use as many command line arguments as needed.
  • We can use the command-line arguments to specify the configuration information while launching our Java application.