1. Introduction
It’s quite common to run applications from the command-line using arguments. Especially on the server-side. Usually, we don’t want the application to do the same thing on every run: we want to configure its behavior some way.
In this short tutorial, we’ll explore how can we handle command-line arguments in Java.
2. Accessing Command-Line Arguments in Java
Since the main method is the entry point of a Java application, the JVM passes the command-line arguments through its arguments.
The traditional way is to use a String array:
public static void main(String[] args) {
// handle arguments
}
However, Java 5 introduced varargs, which are arrays in sheep’s clothing. Therefore, we can define our main with a String vararg:
public static void main(String... args) {
// handle arguments
}
They’re identical, therefore choosing between them is entirely up to personal taste and preference.
The method parameter of the main method contains the command-line arguments in the same order we passed at execution. If we want to access how much arguments did we get, we only have to check the length of the array.
For example, we can print the number of arguments and their value on the standard output:
public static void main(String[] args) {
System.out.println("Argument count: " + args.length);
for (int i = 0; i < args.length; i++) {
System.out.println("Argument " + i + ": " + args[i]);
}
}
Note that in some languages, the first argument will be the name of the application. On the other hand, in Java, this array contains only the arguments.
3. How to Pass Command-Line Arguments
Now that we have an application that handles command-line arguments, we’re eager to try it. Let’s see what options we have.
3.1. Command Line
The most obvious way is the command-line. Let’s assume we already compiled the class com.baeldung.commandlinearguments.CliExample with our main method in it.
Then we can run it with the following command:
java com.baeldung.commandlinearguments.CliExample
It produces the following output:
Argument count: 0
Now, we can pass arguments after the class name:
java com.baeldung.commandlinearguments.CliExample Hello World!
And the output is:
Argument count: 2
Argument 0: Hello
Argument 1: World!
Usually, we publish our application as a jar file, not as a bunch of .class files. Let’s say, we packaged it in the cli-example.jar, and we set com.baeldung.commandlinearguments.CliExample as the main class.
Now we can run it without arguments the following way:
java -jar cli-example.jar
Or with arguments:
java -jar cli-example.jar Hello World!
Argument count: 2
Argument 0: Hello
Argument 1: World!
Note, that Java will treat every argument we pass after the class name or the jar file name as the arguments of our application. Therefore, everything we pass before that are arguments for the JVM itself.
3.2. Eclipse
While we’re working on our application, we’ll want to check if it works the way we want.
In Eclipse, we can run applications with the help of run configurations. For example, a run configuration defines which JVM to use, what is the entry point, the classpath, and so on. And of course, we can specify command-line arguments.
The easiest way to create an appropriate run configuration is to right-click on our main method, then choose Run As > Java Application from the context menu:
With this, we instantly run our application with settings that honor our project settings.
To provide arguments, we should then edit that run configuration. We can do it through the Run > Run Configurations… menu option. Here, we should click the Arguments tab and fill the Program arguments textbox:
Hitting Run will run the application and pass the arguments we just entered.
3.3. IntelliJ
IntelliJ uses a similar process to run applications. It calls these options simply as configurations.
First, we need to right-click on the main method, then choose Run ‘CliExample.main()’:
This will run our program, but it will also add it to the Run list for further configuration.
So, then to configure arguments, we should choose Run > Edit Configurations… and edit the Program arguments textbox:
After that, we should hit OK and rerun our application, for example with the run button in the toolbar.
3.4. NetBeans
NetBeans also falls into line with its running and configuration processes.
We should run our application first by right-clicking on the main method and choosing Run File:
Like before, this creates a run configuration and runs the program.
Next, we have to configure the arguments in that run configuration. We can do that by choosing Run > Set Project Configuration > Customize… Then we should Run on the left and fill the Arguments text field:
After that, we should hit OK and start the application.
4. Third-Party Libraries
Manual handling of the command-line arguments is straightforward in simple scenarios. However, as our requirements become more and more complex, so does our code. Therefore, if we want to create an application with multiple command-line options, it would be easier to use a third-party library.
Fortunately, there’re a plethora of those libraries which support most use cases. Two popular examples are Picocli and Spring Shell.
5. Conclusion
It’s always a good idea to make your application’s behavior configurable. In this article, we saw how to do that using command-line arguments. Additionally, we covered various ways to pass those arguments.
As usual, the examples are available over on GitHub.