DEV Community

Elder Moraes
Elder Moraes

Posted on

Java Train #1 – JDK9 – Modularization

As announced here, I’m now working on this series, where it will be covered the main features of each JDK release. So… let’s do this! This is the first leg of our journey.

JDK 9

Some Java releases are special. I’m almost sure that you’ll agree that JDK 9 is one of them. And if I ask you “why”, you’ll probably give these answers:

  • The Module System, or Project Jigsaw
  • It took 3.5 years to become available (from version 8)

And you are (mostly) right! But… that’s all? Almost 4 years for the modularity on Java?

Of course not. This release is huge! There are exactly 91 features mentioned on the release page of the OpenJDK 9.

So… let’s dig into this version. Of course, I won’t detail all these 91 features. My focus here is to help you improve your code, project, and career. So I’ll cover/mention the features that affect directly one of (or all) those three areas.

At this post, I’ll cover the modularization of the Java Platform.

Project Jigsaw

Ok, I can’t talk about JDK 9 without mention Project Jigsaw. But as it was so extensively explored out there, I’ll give you some specific highlights over it.

Project Jigsaw is a set of many JEPs:

200: The Modular JDK
201: Modular Source Code
220: Modular Run-Time Images
253: Prepare JavaFX UI Controls & CSS APIs for Modularization
261: Module System
275: Modular Java Application Packaging
And one thing that you notice just by looking at these JEPs is that the JDK itself is now modularized. It’s not just a matter of code/packaging organization. This is an important note, as it should drive you to change the way you think about the Java platform.

The modularization system was specified under the JSR 376 and affected:

  • Java Language Specification
  • Java Virtual Machine Specification
  • API Specification

So let’s check a simple example of how you can create your first module.

Coffee App

The paths are very important when working with modules. So, before we begin, create this structure:

It will save you a lot of time and prevent issues.

Now let’s edit the src/coffee.module/coffe/module/Coffee.java file:

Then edit the src/coffee.module/module-info.java like this:

Now we code our Coffee Shop. Edit the src/coffee.shop/coffee/shop/CoffeeShop.java:

And finally the module-info.java for this module (under src/coffee.shop/module-info.java):

Now in the same level as the src folder, you create this folders structure:

Your final tree must be like this:

If it is not, go back and fix it! 😉

Now you are ready to compile and run your Coffee App. Run the commands below in this exact order.

First, we compile our Coffee module:

javac -d mods/coffee.module src/coffee.module/module-info.java src/coffee.module/coffee/module/Coffee.java
Enter fullscreen mode Exit fullscreen mode

Then we compile our CoffeShope module, paying attention to the reference made to the Coffe module:

javac --module-path mods -d mods/coffee.shop src/coffee.shop/module-info.java src/coffee.shop/coffee/shop/CoffeeShop.java
Enter fullscreen mode Exit fullscreen mode

The –module-path mods tell the compiler where to find the modules needed to compile this one.

Now it’s time to order a coffee!

java --module-path mods -m coffee.shop/coffee.shop.CoffeeShop
Enter fullscreen mode Exit fullscreen mode

If you got a message “Enjoy your [CoffeeType]”, then your module is working successfully!

You can check the full source code of this example here: https://github.com/eldermoraes/javatrain/tree/master/01

Don’t forget to left your questions and/other comments that you might have. See you next time!

Top comments (0)