Compiling and Running a Modular Application – Java Module System

19.6 Compiling and Running a Modular Application

Both the javac tool to compile Java source code and the java tool to launch an application include new command-line options to specifically support building of modular applications.

When the javac tool or the java tool is called, initially a module resolution is performed on the application’s structure. This process checks that dependencies among the modules are resolved in the application, thus ensuring a reliable configuration, by catching any problems as early as possible.

The astute reader will soon notice that the tool commands in this chapter are run on a Unix-based platform. Platform-dependent idiosyncrasies are pointed out where appropriate. The first one to note is the line-continuation character to break up a command line over several lines. This is to make the command and its options easier to read. The line-continuation character is a \ (backslash) on the Unix-based platform and a ^ (caret) on the Windows platform. Below, the command on a single line and the command that spans over two lines are equivalent.

>javac One.java Two.java

>javac One.java\
 Two.java

Individual Module Compilation

We first look at how to compile the modules of the adviceApp application individually. Typically, in order to compile a module, the compiler needs to know the source directory to find the source code of the module, the destination directory to place the Java bytecode class files for the module, and any additional modules that the module requires.

The modules in an application can only be compiled in an order that respects the module dependencies—that is, all modules a module requires have already been compiled—or if it does not depend on any other modules. In the case of the advice-App application, there is only one such order, shown in Figure 19.12(b). We can compile the model module first since it does not require any other modules (apart from the java.base module that is readily accessible by default). Figure 19.13 shows how the model module is compiled. The javac command in Figure 19.13(c) shows how the source code of the model module under the src/model directory is compiled to the mods/model directory where the necessary package is created and the class files are placed. Note how the mods/model directory mirrors the src/model directory.

Figure 19.13 Compiling the model Module

Click here to view code image

>javac –module-path mods \
       -d mods/model \
       src/model/module-info.java \
       src/model/com/passion/model/AdviceModel.java

The –module-path option (short form: -p) is used to specify where to find the modules required to compile the source code files specified in the command line. In the case of the model module, this option is superfluous as this module does not depend on any other module. However, the other modules in the adviceApp application do so. As modules are compiled, their exploded modules containing the class files will be readily found in the mods directory specified by this option. This option can also specify a colon-separated (:) list of directories (semicolon (;) separated in Windows) to look up required modules in different directories.

The -d option (no long form) specifies the destination directory (also called target directory) for placing the Java bytecode class files. In this case they will be placed under the mods/model directory, which is created by the compiler if necessary.

The source files in the module are specified last in the command line with their full pathname. The compiler creates the necessary directory hierarchy under the destination directory mods/model specified by the -d option. The module declaration in the module-info.java file is also compiled to a class file, as any other Java source file. This class file is called the module descriptor. It resides immediately under the module root directory, which in this case is mods/model.

After the model module is compiled, the next candidate to compile is the view model, as its required module model has now been compiled to the mods/model directory. Figure 19.14(a) shows the final result of compiling the modules in the adviceApp application by executing the javac commands in the order shown in Figure 19.12(b).

Figure 19.14 Creating Modular JARs

Comments

Leave a Reply

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