Skip to content
Related Articles

Related Articles

Command Line Arguments in Java

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 08 Jun, 2022
Improve Article
Save Article

Java command-line argument is an argument i.e. passed at the time of running the Java program. In the command line, the 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.

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.

Illustration:

Java




// Java Program to Illustrate First Argument
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Printing the first argument
        System.out.println(args[0]);
    }
}


Output:

 

Implementation:

If we run a Java Program by writing the command “java Hello Geeks At GeeksForGeeks” where the name of the class is “Hello”, then it will run up to Hello. It is a command up to “Hello” and after that i.e “Geeks At GeeksForGeeks”, these are command-line arguments.

Example:

Java




// Java Program to Check for Command Line Arguments
 
// Class
class GFG {
 
    // 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)
 
                // Printing command line arguments
                System.out.println(val);
        }
 
        else
 
            // Print statements
            System.out.println("No command line "
                               + "arguments found.");
    }
}


Output

No command line arguments found.

Steps to Run the Above Program are: 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.

Output:

output

This article is contributed by Twinkle Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above. 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!