A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/command-line-arguments-in-java/ below:

Command Line Arguments in Java

Command Line Arguments in Java

Last Updated : 10 Aug, 2025

Java command-line argument is an argument, i.e., passed at the time of running the Java program. Command-line arguments passed from the console can be received by the Java program and used as input.

Example:

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 of first argument

Explanation:

Why Use Command Line Arguments? Working of Command-Line Arguments Example: Display Command-Line Arguments Passed to a Java Program

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

Java
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:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4