Skip to content

🆕 ( Simple Module , Optional , Transitive , Cyclic , Qualified Exports , Module Graph , Observable , Aggregator , Package Naming Conflicts , Module Resolution Process )

Notifications You must be signed in to change notification settings

goxr3plus/java9-modules-tutorial

Repository files navigation

Java 9 ++ modules full tutorial

Everything is tested with JDK 11.0.3

Logging-Sins-793x397

Complete PDF for the tutorial download

I tried to make a simple yet fun, including code examples and pictures tutorial about a hot and important topic which is JPMS(Java Platform Module System) which came in Java 9.

Highly based on java-9-new-features-in-simple-way-jshell-jpms-and-more and Jenkov Tutorials

chrome_2019-06-05_11-35-10

chrome_2019-06-05_11-39-19


chrome_2019-06-05_11-41-00

1. ------------------- Simple module -------------------

chrome_2019-06-05_11-43-36 chrome_2019-06-05_14-47-44 chrome_2019-06-05_14-47-58

Ps i have provided all the code in folders so you don't have to manually type it ;

Compile :

javac --module-source-path simple_module -d out1 -m moduleA

Run :

java --module-path out1 -m moduleA/pack1.Main

2. ------------------- Optional -------------------

chrome_2019-06-05_13-44-20 chrome_2019-06-05_13-48-10 chrome_2019-06-05_13-45-15 chrome_2019-06-05_13-45-28 chrome_2019-06-05_13-47-21

Compile :

javac --module-source-path optional_module -d out2 -m moduleA,moduleB

Run :

java --module-path out2 -m moduleB/pack2.Main

3. ------------------- Transitive -------------------

chrome_2019-06-05_13-36-59 chrome_2019-06-05_13-37-16 chrome_2019-06-05_13-41-54

Be careful not spaces are allowed between (moduleA,moduleB,moduleC)

Compile :

javac --module-source-path transitive_module -d out3 -m moduleA,moduleB,moduleC

Run :

java --module-path out3 -m moduleC/pack3.Main

4. ------------------- Cyclic -------------------

chrome_2019-06-05_11-32-23 chrome_2019-06-05_11-32-32 2019-06-05_13-52-25

Compile :

javac  --module-source-path cyclic_module -d out4 -m moduleA,moduleB

This will produce the following error :

error: cyclic dependence involving moduleA requires moduleA;

chrome_2019-06-05_11-26-05

5. ------------------- Qualified Exports-------------------

chrome_2019-06-05_13-53-55 chrome_2019-06-05_14-47-44 chrome_2019-06-05_14-52-15

Compile :

javac --module-source-path qualified_module -d out5 -m exporterModule,moduleA,moduleB

The error:

 error: package pack2 is not visible
          import pack2.B;
                 ^
        (package pack2 is declared in module exporterModule, which does not export it to module moduleB)
        1 error

How to solve : Well as we can see that error happens because inside our exporterModule we allow access to pack2 only for moduleA :

chrome_2019-06-05_14-42-10

Do you want to fix it ?

Simply export pack2 for moduleB also like this :

export pack2 to moduleA,moduleB;

Okay but now how to run sir ?

 java --module-path out5 -m moduleA/pack1.Test
 java --module-path out5 -m moduleB/pack1.Test

6. ------------------- Observable -------------------

chrome_2019-06-06_18-33-04

Compile :

//TODO

Run :

 //TODO

7. ------------------- Aggregator -------------------

chrome_2019-06-06_18-16-35 chrome_2019-06-06_18-16-48 chrome_2019-06-06_18-17-36 chrome_2019-06-06_18-17-52 chrome_2019-06-06_18-18-59 chrome_2019-06-06_18-19-16 chrome_2019-06-06_18-19-35

Compile :

 javac --module-source-path aggregator_module -d out7 -m aggregator,moduleA,moduleB,moduleC,useModule

Run :

 java --module-path out7 -m useModule/pack4.Main

8. ------------------- Module Graph -------------------

chrome_2019-06-06_10-39-20 chrome_2019-06-06_10-48-39 2019-06-06_10-41-03 chrome_2019-06-06_10-41-31 chrome_2019-06-06_10-41-51 chrome_2019-06-06_10-42-03 chrome_2019-06-06_10-42-12 chrome_2019-06-06_10-42-23

9. ------------ Package Naming Conflicts ------------

chrome_2019-06-10_15-49-59 chrome_2019-06-10_15-50-33 chrome_2019-06-10_15-50-45 chrome_2019-06-10_15-51-00

Compile :

 javac  --module-source-path name_conflicts_module -d out9 -m moduleA,moduleB,useModule

This will produce the following error :

error: module useModule reads package pack1 from both moduleA and moduleB
module useModule{
^
1 error

10. ------------ Module Resolution Process ------------

chrome_2019-06-10_16-06-02 chrome_2019-06-10_16-06-35 chrome_2019-06-10_16-06-51 chrome_2019-06-10_16-07-02 chrome_2019-06-10_16-07-38 chrome_2019-06-10_16-07-46

Compile :

  javac --java-module-path resolution_process_module -d out10 -m useModule,moduleA,moduleB,moduleC,moduleD

Run :

  java --module-path out10 --show-module-resolution -m useModule

About

🆕 ( Simple Module , Optional , Transitive , Cyclic , Qualified Exports , Module Graph , Observable , Aggregator , Package Naming Conflicts , Module Resolution Process )

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages