Compiling and running modules

Let's first compile the HelloApp module and then the HelloClient module. Before running the command, make sure that the Java 9 ClassPath is set. To compile module code, the following command needs to run:

javac -d output com.packt.helloappcompackthelloappHelloApp.java com.packt.helloappmodule-info.java

On successful compilation it will generate HelloApp.class and module-info.class into the output directory.

As our HelloApp module is required by the HelloClient module, we should generate the com.packt.helloapp module jar to include it in the HelloClient module. To create a jar in the mlib folder, run the following jar command:

jar -c -f mlibcom.packt.helloapp.jar -C output .

Now, remove the output directory by running the following command and again make an output directory for the second module:

rmdir /s output

In order to compile the HelloClient module, we need to provide a reference of com.packt.hellpapp.jar  and javac commands and provide a way to pass module-path to refer to other modules. Here we pass the mlib directory as a module path. Without module-path, compilation of the com.packt.hello.client  module is not possible:

javac --module-path mlib -d output com.packt.hello.clientmodule-info.java
javac --module-path mlib -d output com.packt.hello.clientcompackthelloclientHelloClient.java

Now, let's run the module with the help of the following command:

java –-module-path “mlib;output” -m com.packt.hello.client/com.packt.hello.client.HelloClient

The output will be as follows:

At the end of the preceding example, we learned how to create a module and define Dependency Injection in Java modules. The following diagram shows how one module is dependent on another:

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

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