Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Sending Arguments to Applications

Java applications can be run from a command line using a Java interpreter such as the one used behind the scenes by NetBeans. When a Java program is run as a command, the java interpreter loads the application. The command can include extra items of information, as in this example:

java TextDisplayer readme.txt /p

Extra information sent to a program is called an argument. The first argument, if there is one, is provided one space after the name of the application. Each additional argument is also separated by a space. In the preceding example, the arguments are "readme.txt" and "/p".

If you want to include a space inside an argument, you must put quotation marks around it, as in the following:

java TextDisplayer readme.txt /p "Page Title"

This example runs the TextDisplayer program with three arguments: "readme.txt", "/p", and "Page Title". The quote marks prevent "Page" and "Title" from being treated as separate arguments.

You can send as many arguments as you want to a Java application. To do something with them, you must write statements in the application to handle them.

To see how arguments work in an application, create a new class in the Java24 project:

  • Choose File, New File.
  • In the New File wizard, choose the category Java and file type Empty Java File.
  • Give the class the name BlankFiller and hit Finish.

Enter the text of Listing 4.2 into the BlankFiller.java file and save it when you're done. Compile the program, correcting any errors that are flagged by the source editor as you type.

Listing 4.2. The Full Text of BlankFiller.java

1: class BlankFiller {
2:     public static void main(String[] args) {
3:         System.out.println("The " + args[0]
4:             + " " + args[1] + " fox "
5:             + "jumped over the "
6:             + args[2] + " dog."
7:         );
8:     }
9: }

This application compiles successfully and can be run, but if you try it with the menu command Run, Run File, you get a complicated looking error:

Output

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at BlankFiller.main(BlankFiller.java:3)

This error occurs because the program expects to receive three arguments when it is run. You can specify arguments by customizing the project in NetBeans:

  • Choose the menu command Run, Set Project Configuration, Customize.
  • Enter BlankFiller in the Main Class text field.
  • In the arguments field, enter retromingent purple lactose-intolerant and click OK.

Now when you run the application by choosing Run, Run File, the application uses those arguments as adjectives to fill out a sentence, as shown in the following output:

Output

The retromingent purple fox jumped over the lactose-intolerant dog.

Try it with some of your own adjectives, making sure to always include at least three of them.

Arguments are a simple way to customize the performance of a program. The arguments are stored in a type of variable called an array. You learn about arrays during Hour 9, "Storing Information with Arrays."

  • + Share This
  • 🔖 Save To Your Account