Processing Command Line Arguments in Java


Invoking a Program with Command-Line Arguments

For those of you who have worked in Unix or a Command Prompt window under Windows, you have probably entered a command such as:
cd assignment1
		
i.e., a command to change the current working directory to assignment1. The above command actually consists of two components: Note running the command is analogous to calling a function-- in both cases This is not a coincidence-- in essence your program is viewed by the operating system as just another function to be called.

Thus, we see that a program can be invoked with arguments that provide information to the program from the user. We call these command line arguments.

In order for this information to be of any value to the program, we must be able to access the command line arguments from within the program.

The Java Command Line

The Java interpreter command line is slightly different-- here is a typical example of invoking the Java interpreter including command line arguments:
java Finder supermarket testfile.txt
		
(this is an example command line for the example presented below).

We are invoking the Java interpreter, java, passing it the name of the class whose main is to be executed. The actual command line arguments are supermarket and testfile.txt.

Accessing the Command Line Arguments From a Java Program

As we saw above, the Java interpreter command line consists of: The user types the program name and arguments on the command line, and the operating system then parses the command line, breaking it up to it individual parts-- the program name and each argument (where again, for our purposes, the arguments are separated by whitespace). Each of these components-- the program name and the individual arguments-- are treated as a string by the OS, placed into an array of String objects (i.e., String []) and passed to main, just as arguments would be passed to any other function.

Thus, the signature of the main is:

public static void main(String [] args)
		

args therefore is an array of Strings containing the command line arguments in the order in which they appear on the command line. In the example:

java Finder supermarket testfile.txt
		
we have:
args[0]: supermarket
args[1]: testfile.txt
		
The args array is processed just like any other array pf String.

An Example-- Finder

Finder is a program that accepts a string (the program calls it the pattern and an arbitrary number of file names and prints out the names of all those files that contain the string.

The program accepts an arbitrary number of arguments -- the first (args[0] is the pattern, and the rest are the names of the files to be searched.

The program also contains the standard logic for detecting and notifying the user if there are an invalid number of arguments, as well as logic for returning an appropriate exit status (using the method System.exit: 0 if the pattern was found, 1 if it wasn't found in any of the files, and 2 if there were an invalid # of arguments.

Sample Code