1. Computing

Using Command-line Arguments

By

Command-line arguments can be a way of specifying configuration properties for an application. Instead of clicking on an application icon from the operating system, the application is run 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).

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 <pathname> specifies a version of the JDK to be used instead of the default JDK associated with the NetBeans application).

The Main Method

If we examine the main method we can see where the arguments passed to an application appear:

public static void main(String[] args)
{
        
}

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

For example, let's build an application called CommandLineArgs whose only action is to print out the command-line arguments passed out 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);
        }
    }
}
    

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 right-click on the project folder in the Projects window. Choose the Properties option. The Project Properties window will open.  In the Categories list on the right-hand side choose Run. In the Arguments textbox that appears you can 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 applications what the argument is for typically has a hypen or two before it's 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. If you search the internet there are quite a few 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
        {
            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);
            }
        }
    }
}
    

©2014 About.com. All rights reserved.