Previous | Next | Trail Map | Writing Java Programs | Setting Program Attributes


Command-Line Arguments

Your Java application can accept any number of arguments from the command line. Command-line arguments allow the user to affect the operation of an application. For example, an application might allow the user to specify verbose mode--that is, specify that the application display a lot of trace information--with the command-line argument -verbose.


Note: The Java language supports command-line arguments. However, some systems don't normally have command-line arguments. For example, Mac OS doesn't have a command line. Even though the JDK on these systems might provide support for command-line arguments, consider using properties instead so that your programs fit more naturally into the environment.

The user enters command-line arguments when invoking the application. For example, suppose you have a Java application, called Sort, that sorts lines in a file, and that the data you want sorted is in a file named friends.txt. If you are using Windows 95/NTTM, you would invoke the Sort application on your data file like this:

C:\> java Sort friends.txt
In the Java language, when you invoke an application, the runtime system passes the command-line arguments to the application's main method via an array of Strings. Each String in the array contains one of the command line arguments. In the previous example, the command-line arguments passed to the Sort application is an array that contains a single string: "friends.txt".

The Echo Command-Line Arguments Example

This simple application displays each of its command-line arguments on a line by itself:
class Echo {
    public static void main (String[] args) {
        for (int i = 0; i < args.length; i++)
            System.out.println(args[i]);
    }
}
Try this: Invoke the Echo application. Here's an example of how to invoke the application using Windows 95/NT:
C:\> java Echo Drink Hot Java
Drink
Hot
Java
You'll notice that the application displays each word--Drink, Hot, and Java--on a line by itself. This is because the space character separates command-line arguments. If you want Drink Hot Java to be interpreted as a single argument, you would join them with double quotes, which the system consumes. On Windows 95/NT, you would run it like this:
% java Echo "Drink Hot Java"
Drink Hot Java

Conventions for Command-Line Arguments

You should observe several conventions when accepting and processing command-line arguments with a Java application.

Parsing Command-Line Arguments

Many applications that accept command-line arguments allow the user to specify various combinations of arguments in various orders. For example, the UNIX command that prints the contents of a directory -- the ls utility program -- accepts optional arguments that determine which file attributes to list and the order in which to list the files. This flexibility, which can greatly help the user, requires the application to parse the arguments.

Note to C and C++ Programmers: The command-line arguments passed to a Java application differ in number and in type than those passed to a C or C++ program. In C and C++ when you invoke a program, the system passes two parameters to it:
argc--the number of arguments on the command line
argv--a pointer to an array of strings that contain the arguments

When you invoke a Java application, the system only passes one parameter to it:

args--an array of Strings (just an array--not a pointer to an array) that contain the arguments

You can derive the number of command-line arguments with the array's length variable:

numberOfArgs = args.length;
In C and C++, the system passes the entire command line to the program as arguments, including the name used to invoke it. For example, if you invoked a C program as shown below the first argument in the argv parameter is diff:
diff file1 file2
In Java, you always know the name of the application because it's the name of the class where the main method is defined. So the Java runtime system does not pass the class name you invoke to the main method. Rather, the system passes only the items on the command line that appear after the class name. For example, consider the following statement, which invokes a Java application:
java diff file1 file2
The first command-line argument is file1.


Previous | Next | Trail Map | Writing Java Programs | Setting Program Attributes