Javac and Java Katas, Half 2: Module Path – DZone – Uplaza

That is Half 2, a continuation of Javac and Java Katas, Half 1: Class Path, the place we’ll run by the identical workout routines (katas) however this time the principle focus would be the utilization of the Java Platform Module System.

Getting Began

As in Half 1, all instructions on this article are executed inside a Docker container to guarantee that they work and to mitigate any environment-specific setup. 

So,  let’s clone the GitHub repository and run the command under from its java-javac-kata folder:

docker run --rm -it --name java_kata -v .:/java-javac-kata --entrypoint /bin/bash maven:3.9.6-amazoncorretto-17-debian

Kata 1: “Hello, World!” Heat Up

We are going to begin with a primitive Java utility, /module-path-part/kata-one-hello-world-warm-up, which doesn’t have any third-party dependencies. The listing construction is as follows:

Within the image above, we will see the Java venture bundle hierarchy with two courses within the com.instance.kata.one bundle and the module-info.java file which is a module declaration.

Compilation

To compile our code, we’re going to use javac within the single-module mode, which suggests that the module-source-path possibility isn’t used:

javac -d ./goal/courses $(discover -name '*.java')

In consequence, the compiled Java courses ought to seem within the goal/courses folder. The verbose possibility can present extra particulars on the compilation course of:

javac -verbose -d ./goal/courses $(discover -name '*.java')

We will additionally acquire the compiled module description as follows:

java --describe-module com.instance.kata.one --module-path goal/courses

Execution

java --module-path goal/courses --module com.instance.kata.one/com.instance.kata.one.Foremost

What ought to end in Hey World! in your console. Varied verbose:[class|module|gc|jni] choices can present extra particulars on the execution course of:

java -verbose:module --module-path goal/courses --module com.instance.kata.one/com.instance.kata.one.Foremost

Additionally, experimenting a bit throughout each the compilation and execution phases, by eradicating or altering courses and packages, ought to provide you with a very good understanding of which points result in specific errors.

Packaging

Constructing Modular JAR

Based on JEP 261: Module System, “A modular JAR file is like an ordinary JAR file in all possible ways, except that it also includes a module-info.class file in its root directory. ” With that in thoughts, let’s construct one:

jar --create --file ./goal/hello-world-warm-up.jar -C goal/courses/ .

The jar file is positioned within the goal folder. Additionally, utilizing the verbose possibility can provide us extra particulars: 

jar --verbose --create --file ./goal/hello-world-warm-up.jar -C goal/courses/ .

You’ll be able to view the construction of the constructed jar by utilizing the next command: 

jar -tf ./goal/hello-world-warm-up.jar

And get a module description of the modular jar:

jar --describe-module --file ./goal/hello-world-warm-up.jar

Moreover, we will launch the Java class dependency analyzer, jdeps, to achieve much more perception:

jdeps ./goal/hello-world-warm-up.jar

As regular, there’s the verbose possibility, too: 

jdeps -verbose ./goal/hello-world-warm-up.jar

With that, let’s proceed to run our modular jar:

java --module-path goal/hello-world-warm-up.jar --module com.instance.kata.one/com.instance.kata.one.Foremost

Constructing Modular Jar With the Foremost Class

jar --create --file ./goal/hello-world-warm-up.jar --main-class=com.instance.kata.one.Foremost -C goal/courses/ .

Having specified the main-class, we will run our app by omitting the half within the module possibility:

java --module-path goal/hello-world-warm-up.jar --module com.instance.kata.one

Kata 2: Third-Social gathering Dependency

Let’s navigate to the /module-path-part/kata-two-third-party-dependency venture and look at its construction.

This kata can be a Hey World! utility, however with a third-party dependency, guava-30.1-jre.jar, which has an computerized module identify, com.google.frequent. You’ll be able to verify its identify by utilizing the describe-module possibility:

jar --describe-module --file lib/guava-30.1-jre.jar

Compilation

javac --module-path lib -d ./goal/courses $(discover -name '*.java')

The module-path possibility factors to the lib folder that comprises our dependency.

Execution

java --module-path "target/classes:lib" --module com.instance.kata.two/com.instance.kata.two.Foremost

Constructing Modular Jar

jar --create --file ./goal/third-party-dependency.jar --main-class=com.instance.kata.two.Foremost -C goal/courses/ .

Now, we will run our utility as follows: 

java --module-path "lib:target/third-party-dependency.jar" --module com.instance.kata.two

Kata 3: Spring Boot Software Conquest

Within the /module-path-part/kata-three-spring-boot-app-conquest folder, you can see a Maven venture for a primitive Spring Boot utility. To get began with this train, we have to execute the script under.

The primary objective of this script is to obtain all crucial dependencies into the ./goal/lib folder and take away all different information within the ./goal listing.

As seen within the image above, the ./goal/lib has three subdirectories. The take a look at listing comprises all take a look at dependencies. The automatic-module shops dependencies utilized by the module declaration. The remaining dependencies utilized by the appliance are put into the unnamed-module listing. The intention of this separation will turn out to be clearer as we proceed.

Compilation

javac --module-path goal/lib/automatic-module -d ./goal/courses/ $(discover -P ./src/essential/ -name '*.java')

Take discover that for problems, we solely want the modules specified within the module-info.java, that are saved within the automatic-module listing.

Execution

java --module-path "target/classes:target/lib/automatic-module" 
     --class-path "target/lib/unnamed-module/*" 
     --add-modules java.instrument 
     --module com.instance.kata.three/com.instance.kata.three.Foremost

In consequence, it is best to see the appliance working.

For a greater understanding of how the class-path possibility works right here along with the module-path, I like to recommend studying the three.1: The unnamed module a part of “The State of the Module System.”

Constructing Modular Jar

Let’s bundle our compiled code as a modular jar, with the essential class specified:

jar --create --file ./goal/spring-boot-app-conquest.jar --main-class=com.instance.kata.three.Foremost -C goal/courses/ .

Now, we will run it:

java --module-path "target/spring-boot-app-conquest.jar:target/lib/automatic-module" 
     --class-path "target/lib/unnamed-module/*" 
     --add-modules java.instrument 
     --module com.instance.kata.three

Take a look at Compilation

For simplicity’s sake, we’ll use the category path strategy to run exams right here. There’s little profit in scuffling with tweaks to the module system and including extra choices to make the exams work. With that, let’s compile our take a look at code:

javac --class-path "./target/classes:./target/lib/automatic-module/*:./target/lib/test/*" -d ./goal/test-classes/ $(discover -P ./src/take a look at/ -name '*.java')

Take a look at Execution

java --class-path "./target/classes:./target/test-classes:./target/lib/automatic-module/*:./target/lib/unnamed-module/*:./target/lib/test/*" 
     org.junit.platform.console.ConsoleLauncher execute --scan-classpath --disable-ansi-colors

For extra particulars, you’ll be able to take a look at Half 1 of this sequence (linked within the introduction), which elaborates on the theoretical facet of this command.

Wrapping Up

That is it. I hope you discovered this convenient, and that these workout routines have offered you with some sensible expertise relating to the nuances of the Java Platform Module System.

Share This Article
Leave a comment

Leave a Reply

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

Exit mobile version