Open In App

Command Line Arguments in Java

Last Updated : 22 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Java command-line argument is an argument, i.e., passed at the time of running the Java program. In Java, the command line arguments passed from the console can be received in the Java program, and they can be used as input. The users can pass the arguments during the execution, bypassing the command-line arguments inside the main() method.

What are Command Line Arguments?

When we run a Java program, we can give extra information right after the program name. This extra information is called command-line arguments. Let's understand this with the help of an example. If we run this,

java Geeks Hello World

Note: Here, the words Hello and World are the command line arguments. JVM will collect these words and will pass these arguments to the main method as an array of strings called args. The JVM passes these arguments to the program inside args[0] and args[1].

Example: In this example, we are going to print a simple argument in the command line.

Java
// Java Program to Illustrate First Argument
class GFG{

    public static void main(String[] args) {
      
        // Printing the first argument
        System.out.println(args[0]);
    }
}

Output:

Output 1

Explanation: If we run a Java Program by writing the command "java GFG GeeksForGeeks" where the name of the class is "GFG", then it will print GeeksforGeeks. If no arguments are provided (e.g., just java GFG), it will throw an ArrayIndexOutOfBoundsException because the args array is empty.

Why Use Command Line Arguments?

  • It is used because it allows us to provide input at runtime without modifying the whole program.
  • It helps to run programs automatically by giving them the needed information from outside.

Working of Command-Line Arguments

We need to pass the arguments as space-separated values. We can pass both strings and primitive data types(int, double, float, char, etc) as command-line arguments. These arguments convert into a string array and are provided to the main() function as a string array argument.

When command-line arguments are supplied to JVM, JVM wraps these and supplies them to args[]. It can be confirmed that they are wrapped up in an args array by checking the length of args using args.length.

Internally, JVM wraps up these command-line arguments into the args[ ] array that we pass into the main() function. We can check these arguments using args.length method. JVM stores the first command-line argument at args[0], the second at args[1], the third at args[2], and so on.

Example: Display Command-Line Arguments Passed to a Java Program

Java
// Java Program to Check for Command Line Arguments
class Geeks {

    // Main driver method
    public static void main(String[] args)
    {
        // Checking if length of args array is
        // greater than 0
        if (args.length > 0) {

            // Print statements
            System.out.println("The command line"
                               + " arguments are:");

            // Iterating the args array
            // using for each loop
            for (String val : args)
               
                System.out.println(val);
        }
        else
           
            System.out.println("No command line "
                               + "arguments found.");
    }
}

Output:

Output 2

Steps to Run the Above Program

To compile and run a Java program in the command prompt, follow the steps written below.

  • Save the program as Hello.java
  • Open the command prompt window and compile the program- javac Hello.java
  • After a successful compilation of the program, run the following command by writing the arguments- java Hello
  • For example - java Hello Geeks at GeeksforGeeks
  • Press Enter and you will get the desired output.

Important Points:

  • String Input: It takes input in the space separated format.
  • Dynamic-input: Allows user to pass dynamic inputs.
  • Length: We can check the length using .length property.
  • Indexes: We can take the values just using the indexes args[0] args[1]......

Next Article
Article Tags :
Practice Tags :

Similar Reads