GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Command Line Arguments in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Command Line Arguments in Java

Last Updated : 28 Sep, 2026

Command-line arguments in Java are values passed to a program during execution through the command prompt. These arguments are stored as String values in the main() method parameter.

  • Used to provide input without hardcoding values in the program.
  • Helps make programs flexible and reusable for different inputs.
  • Commonly used for file names, configuration values, and user data at runtime.

Command-line arguments are passed after the class name while running the Java program.

Syntax:

java Geeks Hello World

  • The JVM collects these values and passes them to the main(String[] args) method.
  • Hello -> stored in args[0]
  • World -> stored in args[1]
1

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

Java
class GFG {

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

Output:

Output 1
Output of first argument
GeeksForGeeks

Explanation:

  • Running java GFG GeeksForGeeks prints GeeksForGeeks because the argument is passed to main(String[] args).
  • If no arguments are given (e.g., java GFG), it throws ArrayIndexOutOfBoundsException since args is empty.

Working of Command-Line Arguments

  • Command-line arguments in Java are space-separated values passed to the main(String[] args) method.
  • JVM wraps them into the args[] array, where each value is stored as a string (e.g., args[0], args[1], etc.).
  • The number of arguments can be checked using args.length.

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.

  • 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.
Java
class Hello {

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

The command line arguments are:
Geeks
at
GeeksforGeeks

Explanation: When the program is executed using java Hello Geeks at GeeksforGeeks, the JVM stores the three command-line arguments in the args array as args[0] = "Geeks", args[1] = "at", and args[2] = "GeeksforGeeks". Since args.length is greater than 0, the enhanced for loop iterates through the array and prints each argument on a separate line.

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.