Command Line Arguments
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:
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);
}
}Arguments: 2
3 tickets to Pune: Rs. 450Run it like this:
java Fare.java Pune 3How it works
- The terminal splits
Pune 3into two pieces:args[0]is"Pune"andargs[1]is"3". - Line 4 converts
"3"to the number 3 withInteger.parseInt. Arguments are always text. - Line 6 prints
args.length, the number of arguments. - The class or file name (
Fare.java) is not inargs. 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:
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]);
}
}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?
| Use | When |
|---|---|
| Command line arguments | A 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
- Easy: Write a program that prints each of its arguments on its own line, numbered:
1: Pune,2: 3. - Harder: Write
Gst.javathat takes a price and a GST percentage as arguments and prints the total, with a usage message if either is missing.
Key Takeaways
Was this tutorial helpful?