Command Line Arguments

javac -d . lab2/*.java

Command line arguments are arguments that can be given to a program when it is run (on the command line or in Eclipse's Run Dialogue).

The command line arguments are entered on the command line as a space-separated list.

In the following execution command, Jane and Doe are command line arguments.

linux1[35]% java lab2.ScannerInput Jane Doe

The main method takes in one argument: a String array named args[].

public static void main(String args[])
This array will contain any command line arguments that are passed in when we run the program. In this example, args[0] will contain "Jane". Note that in Java, unlike Python or C, the command itself is not stored in args[].

In Eclipse, command line arguments can be passed to the program using the Run Dialogue. Go to Run -> Run Configurations... Highlight the Java Application field and click on the "New launch configuration" icon in the upper left corner. Click on the "Arguments" tab, and your command line arguments can be entered in the "Program Arguments" box.

  1. Create a new class named CommandArgs.
  2. Make it read the first name, last name and age (in words) from the command line.
  3. Give it the same functionality as the "ScannerInput" class.
  4. Run the program from the command line.
  5. Run the program from Eclipse

When you run from the command line, it should look like:

linux1[35]% java lab2.CommandArgs George Washington Two Hundred Eighty
You Entered:
Washington, George, Two Hundred Eighty