Setting Program Attributes |
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 namedfriends.txt
. If you were using Windows 95/NT, you would invoke theSort
application on your data file like this:In the Java language, when you invoke an application, the runtime system passes the command line arguments to the application'sC:\> java Sort friends.txtmain
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 theSort
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:Try this: Invoke the Echo application. Here's an example of how to invoke the application using Windows 95/NT:class Echo { public static void main (String[] args) { for (int i = 0; i < args.length; i++) System.out.println(args[i]); } }You'll notice that the application displays each word--C:\> 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 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 JavaConventions
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--thels
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: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 thenumberOfArgs = args.length;argv
parameter isdiff
: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:diff file1 file2The first command line argument isjava diff file1 file2file1
.
Setting Program Attributes |