Using Command-line Arguments in a Java Application

Arguments Passed to a Java Application Are Processed by Main

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 has a number of startup parameters that can be passed to the application when it is run from a terminal window (e.g., -jdkhome 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:

public static void main(String[] args)
{
 ...do something here 
}

The command-line arguments can be found in the String array called args.

For example, let's consider an application called CommandLineArgs 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:
    Apple
    Banana
    Carrot

    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 -jdkhome.

    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:

    public class CommandLineArgs {
    
     //Command line arguments: 
     // -printout prints out all the arguments after it
     // -addnumbers adds all the number arguments after it
     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!");
     }
     else
     {
      // Set some initial variables
     boolean printout = false;
     boolean addNumbers = false;
     boolean validNumbers = true;
     int total = 0;
    
     for(String argument: args)
     {
     if(argument.equals("-addnumbers"))
     {
     printout = false;
     addNumbers = true;
     }
     else if (argument.equals("-printout"))
     {
     printout = true;
     addNumbers = false;
     }
     else if (addNumbers)
     {
     try {
     total = total + Integer.parseInt(argument);
     } catch (NumberFormatException e) {
     System.out.println("arguments passed with -addnumbers " + "must be integers!");
     validNumbers = false;
     addNumbers = false;
     }
     }
     else if (printout)
     {
     System.out.println(argument);
     }
     }
     if (validNumbers)
     {
     System.out.println("The total of the number arguments is: " + total);
     }
     }
     }
    }
     

    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