Using Command-Line Arguments

Suppose you wish to use the Notepad application in Microsoft Windows. You can, of course, simply type "notepad" in a console window, and the application will launch.

Now suppose that you had already created a text file "C:\myfile.txt" and wanted to print that file to the default printer using Notepad. Rather than launching Notepad and then opening the file from within the program using the menus, and then using the menus again to print the file -- we could do all of this in one step using "command line arguments":

In this case, we could type

notepad /P c:\test.txt

The "/P" and "c:\test.txt" in the command line instruction given above are called command line arguments. "/P" tells the program to print something, while "c:\test.txt" tells the program the name and location of the file with which we want to do something. (Certain command line arguments, like the "/P" above may also be called "switches" or "flags" in some cases)

When we use the java virtual machine to run a java program, we can also specify additional command line arguments that the program can then use to modify how it behaves.

If we had wanted to use a java text-editor we made called MyJavaNotepad instead of Microsoft's notepad application, we might type something similar to the following:

java MyJavaNotepad /P c:\test.txt

As a more general example, to pass the three arguments to a java program named "MyProgram", we use the following syntax:

java myProgram firstArgument secondArgument thirdArgument

Of course, 3 is not a magic number here. We can have any number of arguments for a given program -- the program must just know what to do with them.

Inside our java source code, we can access the command line arguments through the String array called "args". We see this in the main method signature:

public static void main(String[] args)

We will talk more about strings and arrays later, but for now -- know that String refers to an important variable type that can be used to store text (i.e., a "string of characters"), while an array is a special construct that can store in memory many values of the same type. For the purpose of this example, be aware that args[0] stores as a string the first argument passed to the program. args[1] stores as a string the second argument. args[2] stores the third, and so on...

As an example, consider the class Switcheroo shown below:

public class Switcheroo{
  public static void main(String[] args) {
    String s1 = args[0];
    String s2 = args[1];

Here's what happens when we provide arguments of "bob" and "fred" to Switcheroo:

$ java Switcheroo bob fred
fred bob

Note, when you are in an IDE like Eclipse, adding command-line arguments can be a bit more cumbersome. In Eclipse, if you write a program expecting command-line arguments and then just press the "run" button (the green button with a triangle in it), you will get an error. This is your cue to navigate to "Run → Run Configurations" in the menu. Then, making sure that your program is selected under "Java Application" in the navigation pane on the left, click the "Arguments" tab in the right pane. You can type the arguments for the program in the textext area labeled "Program arguments:", or you can type "${string_prompt}" to be prompted for the arguments every time you run the program using the aforementioned green button. The latter method you will probably find to be more useful. Then click the "Apply" and "Run" buttons, respectively.