Setting Program Attributes |
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 namedfriends.txt
, you would run it like this: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 ofjava Sort friends.txtStrings
. EachString
in the array contains one of the command-line arguments. In the previous example, the command-line arguments passed to theSort
application is an array that contains a singleString
: "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:
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:numberOfArgs = args.length;The command-line arguments are in a different font.java diff file1 file2
Echoing Command-Line Arguments
The following simple application displays each of its command-line arguments on a line by itself: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:public class Echo { public static void main (String[] args) { for (int i = 0; i < args.length; i++) System.out.println(args[i]); } }Note that the application displays each word--java Echo Drink Hot Java Drink Hot JavaDrink
,Hot
, andJava
--on a line by itself. This is because the space character separates command-line arguments. If you wantDrink
,Hot
, andJava
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 JavaParsing Numeric Command-Line Arguments
If your program needs to support a numeric command-line argument, it must convert aString
argument that represents a number, such as "34", to a number. Here's a code snippet that converts a command-line argument to anint
:int firstArg; if (args.length > 0) firstArg = Integer.parseInt(args[0]);parseInt
throws aNumberFormatException
if the format ofargs[0]
isn't valid. All of theNumber
classes--Integer
,Float
,Double
, and so on--haveparseXXX
methods that convert aString
representing a number to an object of their type.
Setting Program Attributes |