How to Use Command-Line Arguments in Java
You can pass values to a program using command-line arguments. To run the MySecondApp program and pass the values "value1" and "value2" as input, use the following command:
java com.company.lesson2.MySecondApp value1 value2 These values need to be received within the program. The main method helps us achieve this:
public static void main(String[] args) This method takes an array of type String as a parameter: String[] args. This variable will contain the values we passed: "value1" and "value2".
In the following example, the main() method uses a for loop to iterate over the args array and print the command-line arguments. We will discuss for loops and arrays in more detail in future lessons.
package com.company.lesson2;
public class MySecondApp {
public static void main(String[] args) {
for (String str : args) {
System.out.println("Argument = " + str);
}
}
}
Run the program using the following command:
java com.company.lesson2.MySecondApp value1 value2 Console output:
Argument = value1
Argument = value2 System properties: the -D flag
Command-line arguments are not the only way to pass data into a program at startup. There are also system properties, set with the -D flag in the form -Dname=value. It is important not to confuse them with arguments:
- Arguments come after the class name and land in the
argsarray. - System properties (
-D) come before the class name and are read withSystem.getProperty(), not fromargs.
For example, let's run the program with one system property and one argument:
java -DmyProp=hello com.company.lesson2.MySecondApp value1 Here myProp=hello is a system property, and value1 is a command-line argument. You can read them in code like this:
public class MySecondApp {
public static void main(String[] args) {
System.out.println(System.getProperty("myProp")); // hello
System.out.println(args[0]); // value1
}
} To get all system properties at once, use System.getProperties() — it returns a Properties object with every JVM property, including your own -D values and standard ones such as java.version and os.name.
Don't confuse args and -D
Position matters: anything after the class name is an argument (args); anything before it with the -D prefix is a system property (System.getProperty). If you put -Dprop=val after the class name, it goes into args as a plain string and is not treated as a property.
Frequently Asked Questions
What are command-line arguments in Java?
They are values passed to a program at startup, after the class name, for example java MyApp value1 value2. Inside the program they are available through the String[] args array of the main method.
What type are command-line arguments?
Always String. Even if you pass a number such as 42, it arrives as the string "42". To get a number, you must convert it, for example with Integer.parseInt(args[0]).
How do you pass an argument that contains a space?
Wrap it in quotes: java MyApp "Hello World". Then it becomes a single argument args[0] rather than two.
What is args.length when no arguments are passed?
Zero. In Java args is an empty array (not null), so args.length == 0 and a loop over it simply never runs.
Comments