Which of the following is a feature of using an executable jar file to deploy an application?

This part of the tutorial shows how you can create a distributable application in the IDE and then run that application from outside of the IDE. We will package the application in the form of an executable JAR file.

A JAR file is an archive file that can contain multiple files and folders. JAR files are similar to zip files, but JAR files can have additional attributes that are useful for distributing Java applications. These attributes include digitally signing JAR files, additional compression, multiplatform compatibility, etc.

In this exercise, you create an IDE project and then place two pre-written Java source files into that project. Then you will compile the classes and build an executable JAR file. Afterwards, you will learn how to run the JAR file from outside of the IDE.

The classes used in this tutorial implement features of the GNU grep utility, which can be used for searching text or regular expression patterns inside text files. The project contains both command-line and GUI versions of the application, so that you can see different ways of running the application.

Creating a Project with Existing Sources

  1. Download the DeploymentTutorial.zip file and extract its contents on your system. This zip archive contains source files for the application plus a few other files that will be used in the tutorial.

    1. In NetBeans IDE, choose File > New Project.

    2. In the Choose Category page, select Java Project With Existing Sources in the Java category and click Next.

    3. On the Name and Location page of the wizard, type AnotherGrep as the project name and specify the project’s location. Click Next.

The project folder does not have to be in the same location as the source files that you are importing into the project.

  1. On the Existing Sources page of the wizard, specify the sources that will be in the project. Click the Add Folder button that is to the right of the Source Package Folders field. Navigate to the DeploymentTutorial folder that you have just unzipped on your system, expand the folder, select the src folder, and click Open. The src folder is added to your Source Package Folders field.

  2. Click Finish.

If, for example, you want to exclude some source files from importing into the project, click Next to open the last Includes & Excludes window. In our case, we want to use all the source files in the src folder, so we click Finish to finish working in the New Project wizard.

The project opens in the IDE and becomes visible in the Projects window. You can explore the contents of the project by expanding the project’s Source Packages node, where you should see classes called Grep and xGrep . Grep.java is a console version of the application.

Main-Class: anothergrep.xGrep
0 is a GUI version of the application and uses methods defined in Grep.java .

Configuring the Project

There are a few configuration steps you need to do, such as:

  • Choose the Java platform that will be used to compile the sources.

  • Set the project’s main class. By doing this, you ensure that the JAR file that you create when you build the project is executable.

Verifying the Java Platform

Our project needs to be compiled and run on Java 7 or Java 8 platform. Therefore, you need to make sure that Java 7 or Java 8 is respectively used as the platform for this project.

  1. Right-click the project’s node and choose Properties.

  2. On the Libraries tab, ensure that the Java Platform is JDK 1.7 (or JDK 1.8).

  3. On the Sources tab, choose JDK 7 (or JDK 8) in the Source/Binary format.

  4. Click OK to close the Properties window.

Setting the Main Class

In order for a user to easily run your JAR file (by double-clicking the JAR file or by typing

Main-Class: anothergrep.xGrep
2 at the command line), a main class has to be specified inside the JAR’s manifest file. (The manifest is a standard part of the JAR file that contains information about the JAR file that is useful for the
Main-Class: anothergrep.xGrep
3 launcher when you want to run the application.) The main class serves as an entry point from which the
Main-Class: anothergrep.xGrep
3 launcher runs your application.

When you build a project, the IDE builds the JAR file and includes a manifest. When you set the project’s main class, you ensure that the main class is be designated in the manifest.

To set the project’s main class:

  1. Right-click the project’s node and choose Properties.

  2. Select the Run category and enter

    Main-Class: anothergrep.xGrep
    5 in the Main Class field.

  3. Click OK to close the Project Properties dialog box.

When you build the project later in this tutorial, the manifest will be generated and include the following entry:

Main-Class: anothergrep.xGrep

Building the Project and Creating the JAR File

Now that you have your sources ready and your project configured, it is time to build your project.

  • Choose Run > Build Project (AnotherGrep). Alternatively, right-click the project’s node in the Projects window and choose Build.

When you build your project:

  • Main-Class: anothergrep.xGrep
    6 and
    Main-Class: anothergrep.xGrep
    7 folders are added to your project folder (hereafter referred to as the PROJECT_HOME folder).

  • All of the sources are compiled into

    Main-Class: anothergrep.xGrep
    8 files, which are placed into the
    Main-Class: anothergrep.xGrep
    9 folder.

  • A JAR file containing your project is created inside the AnotherGrep0 folder.

  • If you have specified any libraries for the project (in addition to the JDK), a AnotherGrep1 folder is created in the

    Main-Class: anothergrep.xGrep
    7 folder. The libraries are copied into AnotherGrep3 .

  • The manifest file in the JAR is updated to include entries that designate main class and any libraries that are on the project’s classpath.

You can view the contents of the manifest in the IDE’s Files window. After you have built your project, switch to the Files window and navigate to AnotherGrep4 . Expand the node for the JAR file, expand the AnotherGrep5 folder, and double-click AnotherGrep6 to display the manifest in the Source Editor.

What is the maximum number of finally blocks that you can code?

You can only have one finally clause per try/catch/finally statement, but you can have multiple such statements, either in the same method or in multiple methods. Basically, a try/catch/finally statement is: try. catch (0 or more)

What happens if the user enters ABC at the prompt?

(Refer to code example 16-1) What happens if the user enter "abc" at the prompt? An InputMismatchException is thrown and tryAgain is set to false.