The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Essential Java Classes
Lesson: Setting Program Attributes

Command-Line Arguments

A 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 one invocation. For example, an application might allow the user to specify verbose mode--that is, specify that the application display a lot of trace information. This is done using the command-line argument -verbose.


Purity Tip:  Programs that use command-line arguments are not 100% Pure Java because some systems, like the Mac OS, don't normally have a command line or command-line arguments. Consider using properties instead so that your programs fit more naturally into the environment. If you really must use command-line arguments, have them comply with the POSIX conventions.

The user enters command-line arguments when invoking the application and specifies them after the name of the class to run. For example, suppose you have a Java application, called Sort, that sorts lines in a file. To sort the data in a file named friends.txt, you would run it like this:

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


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. When you invoke a Java application, the system passes only one parameter to it:

args: An array of strings that contains the arguments

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

numberOfArgs = args.length;
In Java, you always know the name of the application because it's the name of the class in which the main method is defined. So the Java runtime system does not pass the class name you invoke to the main method. Rather, it passes only the items on the command line that appear after the class name. For example, consider the following statement, which is used to invoke a Java application:
java diff file1 file2
The command-line arguments are in a different font.

Echoing Command-Line Arguments

The following simple application displays each of its command-line arguments on a line by itself:
public class Echo {
    public static void main (String[] args) {
        for (int i = 0; i < args.length; i++)
            System.out.println(args[i]);
    }
}
Here's an example of how to invoke the application using Windows 95/NT. You enter the words that are shown here in a different font:
java Echo Drink Hot Java
Drink
Hot
Java
Note 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, and Java to be interpreted as a single argument, you would join them by enclosing them within double quotation marks. On Windows 95/NT, you run it like this:
java Echo "Drink Hot Java"
Drink Hot Java

Parsing Numeric Command-Line Arguments

If your program needs to support a numeric command-line argument, it must convert a String argument that represents a number, such as "34", to a number. Here's a code snippet that converts a command-line argument to an int:
int firstArg;
if (args.length > 0)
    firstArg = Integer.parseInt(args[0]);
parseInt throws a NumberFormatException if the format of args[0] isn't valid. All of the Number classes--Integer, Float, Double, and so on--have parseXXX methods that convert a String representing a number to an object of their type.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.