Command-line arguments in Java are used to pass arguments to the main program. If you look at the Java main method syntax, it accepts String array as an argument. When we pass command-line arguments, they are treated as strings and passed to the main function in the string array argument. The arguments have to be passed as space-separated values. We can pass strings and primitive data types as command-line arguments. The arguments will be converted to strings and passed into the main method string array argument.
Let’s say we have a simple java class to print command line arguments values.
package com.journaldev.examples;
public class CommandLineArguments {
public static void main(String[] args) {
System.out.println("Number of Command Line Argument = "+args.length);
for(int i = 0; i< args.length; i++) {
System.out.println(String.format("Command Line Argument %d is %s", i, args[i]));
}
}
}
If we run this class without any arguments, the output will be as follows.
$ java com/journaldev/examples/CommandLineArguments.java
Number of Command Line Argument = 0
Now, let’s pass some arguments to the main class. We have to pass the arguments as space-separated values.
$ java com/journaldev/examples/CommandLineArguments.java "A" "B" "C"
Number of Command Line Argument = 3
Command Line Argument 0 is A
Command Line Argument 1 is B
Command Line Argument 2 is C
$ java com/journaldev/examples/CommandLineArguments.java 1 2 3
Number of Command Line Argument = 3
Command Line Argument 0 is 1
Command Line Argument 1 is 2
Command Line Argument 2 is 3
$
Note: If you are using Java 11 or higher, you don’t need to compile the java source file explicitly. The java command will compile and run the class simultaneously.
We can also pass command-line arguments to a program in Eclipse using Run Configurations.
From the class editor, right click and chose “Run As” -> “Run Configurations…”.
In the pop up window, click on the Arguments tab. Then provide the command line arguments value in the “Program Arguments” text box.
When you will click on the Run button, the run configurations will be saved and the program will execute with the specified command-line arguments.
If you run the class again, the saved run configuration will be used. So if you want to override the command-line arguments or remove them, you will have to open the run configurations window and make necessary changes.
The command-line arguments are used to provide values that are essential to run the program. For example, we can specify the database credentials to be used by the program. We can specify the configuration file location from where the program should pick the required values. Reference: Command-Line Arguments Oracle Docs
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
I want the java program that takes 10 arguments as integer and find the maximum out of it
- Vishu
Thank you! I love this post. That help me understand clearly!
- Zanik