Creating JAR Files – Java Module System

19.7 Creating JAR Files

A JAR (Java Archive) file is a convenient way of bundling and deploying Java executable code and any other resources that are required (e.g., image or audio files). A JAR file is created by using the jar tool. The jar command has many options, akin to the Unix tar command (p. 1221).

In addition to creating plain JARs (also referred to as non-modular JARs), the jar tool of the JDK has been enhanced to create and manipulate modular JARs, making it convenient to bundle and deploy modules. The procedure for creating a plain or a modular JAR is the same; the main difference being that a modular JAR always contains the module descriptor—that is, the module-info.class file.

Creating Modular JARs

Figure 19.14(b) shows the jar commands that can be used to create a modular JAR for each of the modules in the adviceApp application. The resulting modular JARs are placed under the mlib directory, as shown in Figure 19.14(c). We take a closer look at the jar command to create a modular JAR for the model module:

>jar –verbose \
     –create \
     –file mlib/model.jar \
     -C mods/model .

Possible output to the terminal:

Click here to view code image

added manifest
added module-info: module-info.class
adding: com/(in = 0) (mods= 0)(stored 0%)
adding: com/passion/(in = 0) (mods= 0)(stored 0%)
adding: com/passion/model/(in = 0) (mods= 0)(stored 0%)
adding: com/passion/model/AdviceModel.class(in = 641) (mods= 415)(deflated 35%)

The –verbose option (short form: -v) instructs the jar tool to print information about its operation on the terminal, as evident from the output on the terminal.

The –create option (short form: -c) results in a modular JAR to be created, as the module descriptor will be included in the JAR file (see below).

The –file option (short form: -f) specifies the file name of the modular JAR. In this case, it is model.jar that will be created and located under the mlib directory. Note there are no restrictions on the file name of the modular JAR, as long as it is a legal file name. Of course, having a descriptive file name that reflects what the module in the modular JAR stands for can aid readability.

The -C option instructs the jar tool to change to the mods/model directory and include the contents of this directory which is designated by the dot (.) special notation. From the output on the terminal we can see what is added to the JAR file. Note that the modular JAR does not include information about the module root directory (in the above case, mods/model) in the modular JAR because the module descriptor already has information about the name of the module. The contents included in the modular JARs are shown by the dashed boxes in Figure 19.14(a).

The observant reader will notice that the jar command in Figure 19.14(b) to create the main.jar has an extra option to specify the application entry point:

Click here to view code image

>jar –verbose \
     –create \
     –file mlib/main.jar \
     –main-class com.passion.main.Main \
     -C mods/main .

Since the entry point of the application is in the model module, the –main-class option (short form: -e) specifies the fully qualified name of the class containing the main() method—that is, com.passion.main.Main. The module name is not specified.

Using the short form of the first four options in the above jar command, respectively (see Table 19.6), the command can be written as follows:

Click here to view code image

>
jar -vcfe mlib/main.jar com.passion.main.Main -C mods/main .

Note that if the files named module-info.class are deleted in Figure 19.14(a), the jar commands in Figure 19.14(b) will still create JAR files, but these will be plain JARs, and not modular JARs.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *