Let's get started with a Microservice Architecture with Spring Cloud:
Command-Line Arguments in Java
Last updated: December 27, 2025
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 somehow.
In this short tutorial, we’ll explore how we can 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 in 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 arguments of our application. Therefore, everything we pass before that is our argument 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. Passing a File Path as an Argument in Java
In a scenario where we need to pass a file path as a command-line argument, we need to provide the absolute path of the file using the correct path separator for the operating system. For instance, Windows typically uses backslashes (\), while Linux uses forward slashes (/).
Let’s confirm the default file separator for the current operating system:
FileSystems.getDefault().getSeparator()
Next, let’s write a simple program that outputs the content of a file to the console:
public class CliFileReader {
public static void main(String[] args) throws IOException {
if (args.length == 0) {
System.out.println("Usage: CliFileReader <file-path>");
}
String path = args[0];
List<String> lines = Files.readAllLines(Path.of(path));
for (String line : lines) {
System.out.println(line);
}
}
}
The code above expects the file path as a command-line argument. If no file path is provided, it throws an exception and prints the usage instructions instead of attempting to read the file.
Let’s place a file named hello.txt in our resources folder and pass its absolute path as an argument in our IDE run configuration:
"/home/baeldung/tutorials/core-java-modules/core-java-lang/src/main/resources/hello.txt"
After defining the file path as an argument, the program prints the content of the file to the console.
Alternatively, we can package the program as a JAR file and run it directly from the command line by passing the file path argument:
$ java -jar CliFileReader.jar /home/baeldung/tutorials/core-java-modules/core-java-lang/src/main/resources/hello.txt
This allows the program to run independently of an IDE.
5. 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’s a plethora of those libraries that support most use cases. Two popular examples are Picocli and Spring Shell.
6. Conclusion
In this article, we saw how to do that using command-line arguments. Additionally, we covered various ways to pass those arguments.
The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.

















