Command-Line Arguments In Java

In this tutorial we’ll look at how to execute Java programs passing command-line arguments.

A common practice in Java is to run programs with command-line arguments. This technique is commonly applied in different scenarios, such as when launching GUI Applications, Client Applications, Server Applications, and background jobs.

In this tutorial, we’ll look at how to read command-line arguments from Java programs, and also some approaches to pass command-line arguments to Java applications.

Reading Command-Line Arguments In Java

As you probably already know the main method as it’s commonly referred, is the entry point to most Java applications.

1
2
3
4
public static void main(String[] args) {


}

This method as shown above has a single parameter which accepts an array of Strings. However, starting from Java 5 it can also be rewritten using varargs.

1
2
3
public static void main(String... args) {
   System.out.println("Cool, Java 5 Has Varargs");
}

Varargs provide some syntactic sugar to Java, by allowing you to pass one or more method arguments of a certain type as a comma separated list of String values.

So instead of doing:

1
callMethod(new String[]{"1","2","3"})

We can do:

1
main("1","2","3");

A varargs parameter can then be treated as an array from the block in which it can be accessed. Therefore, we can define a main method that takes an array of Strings or a varargs of String, but in the end we treat both as arrays.

Command-line arguments passed to Java applications can be accessed from this parameter.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class PrintCommandLineArguments {

    public static void main(String[] args) {
        System.out.println("Command-Line Argument Count: " + args.length);

        for (int i = 0; i < args.length; i++) {
            System.out.println("Command-Line Argument " + i + ": " + args[i]);
        }
    }
}

Above we access the number of command-line arguments using the length property of the args parameter. We also iterate the arguments using a for loop and print them to the console.

How To Pass Command-Line Arguments

There are various approaches to passing command-line arguments to a Java program. In this section we will examine the most popular methods.

From The Command-Line

The most common approach is to compile the Java class and execute it from the command line, passing the command line arguments.

1
2
classes> java com.stephenenright.tutorials.corejava.helloworld.PrintCommandLin
eArguments

If we execute the above program it outputs:

1
Command-Line Argument Count: 0

as we passed zero arguments. Now to pass the command-line arguments it is as simple as typing a space separated list of parameters after the Java class name.

1
2
classes> java com.stephenenright.tutorials.corejava.helloworld.PrintCommandLin
eArguments 1 2 3 4

Running the above command outputs:

1
2
3
4
5
Command-Line Argument Count: 4
Command-Line Argument 0: 1
Command-Line Argument 1: 2
Command-Line Argument 2: 3
Command-Line Argument 3: 4

But what happens if one of our command-line arguments contains a space. Well, in this case we have to escape the argument.

1
2
classes> java com.stephenenright.tutorials.corejava.helloworld.PrintCommandLin
eArguments "Good Morning" Stephen

Above we want to pass the String Good Morning as the first command-line argument. To do this we escape it by using double quotes.

1
2
3
Command-Line Argument Count: 2
Command-Line Argument 0: Good Morning
Command-Line Argument 1: Stephen

Executing A Jar File On The Command-Line

A jar file is used for packaging Java applications. Therefore, it is a common task to pass command-line arguments to a class packaged inside a jar file.

1
java -jar myExample.jar "Good Morning" Stephen

Above both arguments will be passed to the class designated as the entry point via the Main-Class attribute in the jar files MANIFEST.MF file. Alternatively, we could also pass the arguments to a class with a main method by placing the fully qualified class name after the jar file name.

1
2
java -cp myExample.jar com.stephenenright.tutorials.corejava.helloworld.PrintCommandLin
eArguments "Good Morning" Stephen

Complex Command-Line Arguments

When dealing with command-line arguments in more complicated situations, such as when dealing with named and optional arguments. It is worthwhile to use a third-party library rather than wasting time reinventing the wheel. Some of the third party libraries include:

  • Picocli
  • Spring Shell

Conclusion

In this short tutorial we examined how to run Java applications with command-line arguments.