Java Train #1 – JDK9 – Modularization
Tags: java, release, blog, javatrain
Published: 2020-01-15 17:03:01

---
title: Java Train #1 – JDK9 – Modularization
published: true
description: 
tags: Java, Release, Blog, JavaTrain
series: Java Train
---
![](https://i1.wp.com/eldermoraes.com/wp-content/uploads/2019/11/fabrizio-verrecchia-BXwbfuM0cdk-unsplash.jpg?resize=300%2C255&ssl=1)

As announced [here](https://dev.to/elderjava/i-want-to-catch-the-java-train-5bh4), 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:

![](https://i1.wp.com/eldermoraes.com/wp-content/uploads/2020/01/tree.png?w=330&ssl=1)

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

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

{% gist https://gist.github.com/eldermoraes/0c3fa0ce9374fd4a831ba4eef47e36dd %}

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

{% gist https://gist.github.com/eldermoraes/732cb7f4357308a6dce931fc18978970 %}

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

{% gist https://gist.github.com/eldermoraes/e9688ca1175bd8e0acbade299b6d2d25 %}


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

{% gist https://gist.github.com/eldermoraes/3e3f3a4dbab12106db974690c1a58616 %}


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

![](https://i0.wp.com/eldermoraes.com/wp-content/uploads/2020/01/tree-mods.png?zoom=1.25&resize=229%2C151&ssl=1)

Your final tree must be like this:

![](https://i1.wp.com/eldermoraes.com/wp-content/uploads/2020/01/tree-full.png?w=334&ssl=1)

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
```

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
```

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
```

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!