Using Command-Line Arguments in a Java Application

Arguments passed to a Java application are processed by main

Illustration of coding

bijendra/Getty Images

Command-line arguments can be a way of specifying configuration properties for an application, and Java is no different. Instead of clicking on an application icon from the operating system, you can run the Java application from a terminal window. Along with the application name, a number of arguments can follow which are then passed to the application's starting point (i.e., the main method, in the case of Java).

For example, NetBeans (an Integrated Development Environment) has a number of startup parameters that can be passed to the application when it is run from a terminal window (e.g.,

specifies a version of the JDK to be used instead of the default JDK associated with the NetBeans application).

The Main Method

Let's examine the main method to see where the arguments passed to an application appear:

The command-line arguments can be found in the

called

For example, let's consider an application called

whose only action is to print out the command-line arguments passed to it:

public class CommandLineArgs {
   public static void main(String[] args) {
//check to see if the String array is empty
if (args.length == 0)
{
System.out.println("There were no commandline arguments passed!");
}
       //For each String in the String array
//print out the String.
for(String argument: args)
{
System.out.println(argument);
}
}

Syntax of Command Line Arguments

The Java Runtime Engine (JRE) expects arguments to be passed following a particular syntax, like so:

java ProgramName value1 value2

Above, "java" invokes the JRE, which is followed by the name of the program you are calling. These are followed by any arguments to the program. There is no limit to the number of arguments a program can take, but the order is critical. The JRE passes the arguments in the order in which they appear on the command line. For example, consider this code snippet from above:

public class CommandLineArgs2 {​
   public static void main(String[] args) {
if (args.length == 0)
{
System.out.println("There were no commandline arguments passed!");
}

When arguments are passed to a Java program, args[0] is the first element of the array (value1 above), args[1] is the second element (value2), and so on. The code args.length() defines the length of the array.

Passing Command-Line Arguments

In NetBeans, we can pass command-line arguments without having to build the application and run it from a terminal window. To specify the command-line arguments:

  1. Right-click on the project folder in the
    Projects
    window.
  2. Choose the
    Properties
    option to open 
    Project Properties
    window. 
  3. In the
    Categories
    list on the right-hand side, choose
    Run
  4. In the
    Arguments
    textbox that appears, specify the command-line arguments you want to pass to the application. For example, if we enter
    Apple Banana Carrot
    in the
    Arguments
    textbox and run the
    CommandLineArgs
    program listed above, we will get the output:

Parsing the Command-Line Arguments

Typically, a command line argument is passed with some information about what to do with the value being passed. The argument informing the application what the argument is for typically has a hyphen or two before its name. For example, the NetBeans example for the startup parameter specifying the JDK path is

This means you'll need to parse the command-line arguments to figure out what to do with the values. There are several Java command-line frameworks for parsing command-line arguments. Or you could write a simple command-line parser if the arguments you need to pass are not that many:

The code above either prints the arguments or add them together if they are integers. For example, this command line argument would add the numbers:

java CommandLineArgs -addnumbers 11 22 33 44
Format
mla apa chicago
Your Citation
Leahy, Paul. "Using Command-Line Arguments in a Java Application." ThoughtCo, Jun. 1, 2021, thoughtco.com/using-command-line-arguments-2034196. Leahy, Paul. (2021, June 1). Using Command-Line Arguments in a Java Application. Retrieved from https://www.thoughtco.com/using-command-line-arguments-2034196 Leahy, Paul. "Using Command-Line Arguments in a Java Application." ThoughtCo. https://www.thoughtco.com/using-command-line-arguments-2034196 (accessed March 28, 2024).