Command Line Arguments in Java W3Schools Guide

Robert Jul 13, 2026

Command line arguments in Java are a powerful tool that assists developers in persuading the JVM to configure an application at runtime. They allow you to pass arguments from the command line to your Java program, making your applications more flexible and adaptable to different environments.

What are Command Line Arguments in C?
What are Command Line Arguments in C?

A command line argument is simply a string that is passed to the Java interpreter when the Java application is started. They are represented in the program by an array of strings known as args[].

Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide

Passing Command Line Arguments

The syntax to pass command line arguments in Java is straightforward:

Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually

java YourClassName arg1 arg2 arg3 ...

Using args[]

🚀 5 Essential Java Methods Every Developer Should Know! 💻
🚀 5 Essential Java Methods Every Developer Should Know! 💻

The args[] array is available within your program via the Main method:

public static void main(String[] args) {
    System.out.println("You have passed " + args.length + " arguments");
}

Accessing individual arguments

You can access each argument individually using its index:

🔥 Java try-catch Explained! 🚀
🔥 Java try-catch Explained! 🚀

for (int i = 0; i < args.length; i++) {
    System.out.println("Argument " + i + ": " + args[i]);
}

Java Command Line Arguments Examples

Here are a couple of examples of passing command line arguments in Java:

Example 1: Simple argument passing

Java Conditional Statements Cheat Sheet
Java Conditional Statements Cheat Sheet

Let's create a simple Java program that accepts a name as a command line argument and greets the user:

public class Greeter {
    public static void main(String[] args) {
        System.out.println("Hello, " + args[0] + "!");
    }
}

If you run this program with java Greeter World, it will print Hello, World!

a cell phone with an image of a programming program on the screen and text that reads,
a cell phone with an image of a programming program on the screen and text that reads,
🚀 The Ultimate Guide to Java Method Overloading 🎯
🚀 The Ultimate Guide to Java Method Overloading 🎯
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
JAVA Commands Chart 8 x 10 Digital Download
JAVA Commands Chart 8 x 10 Digital Download
Example for public in java
Example for public in java
the world's most famous lines are shown in this diagram, which shows how many different
the world's most famous lines are shown in this diagram, which shows how many different
📘 Must-Know Java Keywords Explained Simply 🔑
📘 Must-Know Java Keywords Explained Simply 🔑
Java if...else (With Examples)
Java if...else (With Examples)
Layout en Java. BorderLayout
Layout en Java. BorderLayout
JavaScript | Chapter 2 📖| Part 1 ✨
JavaScript | Chapter 2 📖| Part 1 ✨
Multi-line strings in JavaScript and Node.js
Multi-line strings in JavaScript and Node.js
[Solved] How to count Vowels and Consonants in Java String Word? Example
[Solved] How to count Vowels and Consonants in Java String Word? Example
Keywords in Java - Java Reserved Words - TechVidvan
Keywords in Java - Java Reserved Words - TechVidvan
Top Features of Java Every Beginner Should Know ☕🚀
Top Features of Java Every Beginner Should Know ☕🚀
Command Line Basics Every Beginner Should Learn
Command Line Basics Every Beginner Should Learn
the key words in this language are used to describe something
the key words in this language are used to describe something
🔥 Nested If in Java: Decisions Inside Decisions!
🔥 Nested If in Java: Decisions Inside Decisions!
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne

Example 2: Complex data structures

Command line arguments can also be used to pass complex data structures like numbers or dates:

`.command`!

Experimental with command line arguments in Java can help you build more resilient and versatile applications. It's a key skill that every Java developer should master.

So, why not give it a try? Start with simple examples and gradually advance to more complex ones. You'll be surprised at the flexibility command line arguments can offer to your Java applications.