Home > Articles

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

Sending Arguments to Applications

You can run Java applications from a command line using java, a program that invokes the JVM. NetBeans uses this program behind the scenes when you run programs. When a Java program is run as a command, the JVM loads the application. The command can include extra items of information, as in this example:

java TextDisplayer readme.txt /p

Any 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 also is 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 quotation marks prevent Page and Title from being treated as separate arguments.

You can send as many arguments as you want to a Java application (within reason). 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:

  1. Choose File, New File.

  2. In the New File Wizard, choose the category Java and file type Empty Java File.

  3. Give the class the name BlankFiller, the package com.java24hours, and click Finish.

Enter the text of Listing 4.2 in the source code editor and save it when you’re done. Compile the program, correcting any errors that are flagged by the editor as you type.

LISTING 4.2The Full Text of BlankFiller.java

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

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:

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:

  1. Choose the menu command Run, Set Project Configuration, Customize. The Project Properties dialog opens.

  2. Enter com.java24hours.BlankFiller in the Main Class text field.

  3. In the Arguments field, enter retromingent purple lactose-intolerant and click OK.

Because you’ve customized the project, you must run it a little differently. Choose the menu command Run, Run Main Project. The application uses the arguments you specified as adjectives to fill out a sentence, as shown in Figure 4.2.


FIGURE 4.2 The output of the BlankFiller application.

Return to the Project Properties dialog and designate three adjectives of your own choosing as arguments, making sure to always include at least three.

Arguments are a simple way to customize the behavior 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