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. Show 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
The project folder does not have to be in the same location as the source files that you are importing into the project.
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 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 0 is a GUI version of the application and uses methods defined in Grep.java .Configuring the ProjectThere are a few configuration steps you need to do, such as:
Verifying the Java PlatformOur 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.
Setting the Main ClassIn order for a user to easily run your JAR file (by double-clicking the JAR file or by typing 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 3 launcher when you want to run the application.) The main class serves as an entry point from which the 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:
When you build the project later in this tutorial, the manifest will be generated and include the following entry:
Building the Project and Creating the JAR FileNow that you have your sources ready and your project configured, it is time to build your project.
When you build your project:
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 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.
|