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

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 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 (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 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.2. The Full Text of BlankFiller.java


 1: class BlankFiller {
2:     public static void main(String[] arguments) {
3:         System.out.println("The " + arguments[0]
4:             + " " + arguments[1] + " fox "
5:             + "jumped over the "
6:             + arguments[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:

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

2. Enter 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 the following output:

Output


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


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.”

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.29.22