Command Line Arguments

Naveen AnamaneniUpdated · 4 min read

Some programs ask questions while they run. Others get everything they need before they start: ping google.com, git commit -m "fix". The values typed after the program's name are command line arguments. Every Java program can receive them through the String[] args parameter of main, which the lesson "The Main Method in Java" introduced. This lesson shows how to use them properly.

From the Command Line to args

The terminal splits everything after the class name at spaces, and Java puts each piece into the args array, in order:

How a command line becomes the args array
Fare.java▶ Try it
public class Fare {
    public static void main(String[] args) {
        String city = args[0];
        int tickets = Integer.parseInt(args[1]);
        int total = tickets * 150;
        System.out.println("Arguments: " + args.length);
        System.out.println(tickets + " tickets to " + city + ": Rs. " + total);
    }
}
Output
Arguments: 2
3 tickets to Pune: Rs. 450

Run it like this:

Terminal
java Fare.java Pune 3

How it works

  1. The terminal splits Pune 3 into two pieces: args[0] is "Pune" and args[1] is "3".
  2. Line 4 converts "3" to the number 3 with Integer.parseInt. Arguments are always text.
  3. Line 6 prints args.length, the number of arguments.
  4. The class or file name (Fare.java) is not in args. Unlike C, args[0] is the first real argument.

Try it yourself: run it with Mumbai 5, then with "Navi Mumbai" 5. What does args.length show each time?

Values with Spaces

The terminal splits at spaces, so java Fare.java New Delhi 2 gives three arguments: New, Delhi and 2. Put a value with spaces in double quotes to keep it together: java Fare.java "New Delhi" 2. The quotes themselves aren't passed to Java.

Check Before You Use

A program that reads args[0] without checking crashes when the user forgets an argument:

SafeFare.java▶ Try it
public class SafeFare {
    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("Usage: java SafeFare.java <city> <tickets>");
            return;
        }
        int tickets = Integer.parseInt(args[1]);
        System.out.println(tickets + " tickets to " + args[0]);
    }
}
Output
Usage: java SafeFare.java <city> <tickets>

This example ran with no arguments, so it printed a usage message and stopped with return. That's the standard, friendly way for command-line tools to explain how to use them.

Arguments in an IDE

In IntelliJ IDEA, open Run → Edit Configurations, select your program, and type the arguments into Program arguments, exactly as you would after the class name: Pune 3. Eclipse and VS Code have the same setting under their run configurations.

Arguments, Input or a File?

UseWhen
Command line argumentsA few values known before the program starts; scripts and automation
Keyboard input (Scanner, IO.readln)Values the user decides while the program runs
A file (covered later, with file handling)Lots of data, or settings that rarely change

Common Mistakes

Expecting args[0] to be the program name. In Java it's the first argument after the class or file name.

Doing arithmetic on arguments without converting. args[1] + 1 with "3" gives "31".

Forgetting quotes around values with spaces. New Delhi becomes two arguments.

Practice

  1. Easy: Write a program that prints each of its arguments on its own line, numbered: 1: Pune, 2: 3.
  2. Harder: Write Gst.java that takes a price and a GST percentage as arguments and prints the total, with a usage message if either is missing.

Quick check: Command line arguments

4 questions
1You run java Fare.java Pune 3. What is args[0]?
2What is the type of args[1] in the same command?
3How many arguments does java Fare.java New Delhi 2 pass?
4What is args.length when you pass no arguments?

Key Takeaways

Was this tutorial helpful?

Lesson 52 of 482