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.

When invoking an application, the user types the command line arguments after the application name. For example, suppose you had a Java application, called Sort, that sorted lines in a file, and that the data you want sorted is in a file named friends.txt. If you were using Windows 95/NT, 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".

Echo Command Line Arguments

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

There are several conventions that you should observe when accepting and processing command line arguments with a Java application.

Parsing Command Line Arguments

Most applications accept several command line arguments that allow the user to affect the execution of the application. For example, the UNIX command that prints the contents of a directory--the ls utility program--accepts arguments that determine which file attributes to print and the order in which the files are listed. Typically, the user can specify the command line arguments in any order thereby requiring the application to parse them.

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, if you invoked a Java application like this:
java diff file1 file2
The first command line argument is file1.


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