That is Half 2, a continuation of Javac and Java Katas, Half 1: Class Path, the place we are going to run by the identical workouts (katas) however this time the primary 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 be sure 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’ll begin with a primitive Java software, /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 are able to see the Java challenge 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 means that the module-source-path
choice shouldn’t be used:
javac -d ./goal/courses $(discover -name '*.java')
In consequence, the compiled Java courses ought to seem within the goal/courses
folder. The verbose
choice can present extra particulars on the compilation course of:
javac -verbose -d ./goal/courses $(discover -name '*.java')
We are able to additionally receive 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.Principal
What ought to end in Howdy World!
in your console. Numerous 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.Principal
Additionally, experimenting a bit throughout each the compilation and execution levels, by eradicating or altering courses and packages, ought to offer you understanding of which points result in explicit errors.
Packaging
Constructing Modular JAR
In response to 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
choice may give 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 are able to launch the Java class dependency analyzer, jdeps, to realize much more perception:
jdeps ./goal/hello-world-warm-up.jar
As typical, there’s the verbose
choice, 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.Principal
Constructing Modular Jar With the Principal Class
jar --create --file ./goal/hello-world-warm-up.jar --main-class=com.instance.kata.one.Principal -C goal/courses/ .
Having specified the main-class
, we are able to run our app by omitting the
half within the module
choice:
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 challenge and look at its construction.
This kata can be a Howdy World!
software, 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 examine its identify by utilizing the describe-module
choice:
jar --describe-module --file lib/guava-30.1-jre.jar
Compilation
javac --module-path lib -d ./goal/courses $(discover -name '*.java')
The module-path
choice 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.Principal
Constructing Modular Jar
jar --create --file ./goal/third-party-dependency.jar --main-class=com.instance.kata.two.Principal -C goal/courses/ .
Now, we are able to run our software 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, one can find a Maven challenge for a primitive Spring Boot software. To get began with this train, we have to execute the script under.
The primary objective of this script is to obtain all needed dependencies into the ./goal/lib
folder and take away all different recordsdata within the ./goal
listing.
As seen within the image above, the ./goal/lib
has three subdirectories. The check
listing comprises all check dependencies. The automatic-module
shops dependencies utilized by the module declaration. The remaining dependencies utilized by the applying 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/most important/ -name '*.java')
Take discover that for issues, 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.Principal
In consequence, you need to see the applying operating.
For a greater understanding of how the class-path
choice 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 most important
class specified:
jar --create --file ./goal/spring-boot-app-conquest.jar --main-class=com.instance.kata.three.Principal -C goal/courses/ .
Now, we are able to 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 are going to use the category path method to run exams right here. There’s little profit in fighting tweaks to the module system and including further choices to make the exams work. With that, let’s compile our check code:
javac --class-path "./target/classes:./target/lib/automatic-module/*:./target/lib/test/*" -d ./goal/test-classes/ $(discover -P ./src/check/ -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 possibly can 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 handy, and that these workouts have offered you with some sensible expertise concerning the nuances of the Java Platform Module System.