Introduction to Java 9 modules with example

Published by Vignesh M on

Java 9 is a major feature release of the Java platform and the key features include Java Platform Module System(JPMS), Java Shell and lot of improvements to JVM and core libraries. In this post, let’s have a look at the Java 9 modules and write programs to get started in a modular way.

Why Java needs modularity?

Java needs the concept of module system to address two main problems.

  1. Classpath problem
  2. Monolithic JDK

Classpath problem:

Classpath have the following problems which can make them error prone.

  •  When two different versions of same library deployed accidentally. This could make the application buggy and to behave in an unexpected way. Debugging the issue could be challenging.
  • Conflicts arising when there are two different libraries and have the same contents. Again this could make way for some nasty bugs.
  • Classpath helps to find the classes irrespective of the components/packages to which they belong. Jar files are just containers of classes and there is no control on restricting the access to packages.

Monolithic JDK:

In the initial stages of Java, the JDK is relatively small. Well, but as Java evolves it means JDK grows. The size of rt.jar with over 60MB in jdk1.8 could very well state the problem.

  • The monolithic nature of JDK makes it challenging to use in memory intensive and IoT devices. Because, eventhough we might need only a small portion of JDK, we have to load them entirely to use them.
  • Using JDK in Cloud environment. Yes, Cloud do offers flexibility in using the space to hold the application but we might end up using lot of space for JDK itself when we actually need only a small portion of it.

Apart from this, restricting the access to Java internal API’s and the startup performance of Java applications are other problems to mention.

The newly introduced Modules helps Java to tackle the above mentioned problems.

What is a Module?

Module is a container of packages designed for reuse. Each module contains module descriptor which includes the below information.

  • Module name
  • Module dependencies i.e name of the other modules which it depends on
  • Name of the packages it exports which can be used only by the modules which depends on it

Module declaration

Module is declared inside the file named "module-info.java" which is the module descriptor.

Develop a Java module

Let’ develop a simple Java module, compile and execute it.

You can download the example from here. Also download link is available at the end of this post.

Step 1: Prerequisites 

Download and install the jdk9 from here based on your operating system. Verify whether the Java is correctly installed.

Step 2: Create project structure

Create the project structure using the below commands.

Step 3: Create a Module folder

Create a module under src folder. For this example, let’s create a module named com.rayfocus.app

Step 4: Create Module descriptor

Module descriptor always have the name module-info.java. Let’s create one under the module directory com.rayfocus.app

Add the below contents to module-info.java

Step 5: Develop a Hello World Module

Create a simple Java class under the package com.rayfocus.app to print the Hello Java9 Modules to console. First create a package structure com.rayfocus.app

Add the below contents to Client.java

Step 6: Compile Module

Compile the module using javac as below. To place our compiled class files let’s create another folder mods under the project directory java9-modules.

Step 7: Project structure tree

After successful compilation, the module project created so far will have the following structure.

Step 8: Run the program

Run the program com.rayfocus.app.Client using the below command.

Congrats! You have created a Java module, compiled and executed it successfully. Let’s continue to develop a multiple module and see how the implied readability works in Java.

Develop multiple modules

Add two more modules com.rayfocus.services and com.rayfocus.domain .

Step 1: Module – com.rayfocus.services

Create module com.rayfocus.services as below.

Add the below contents to module-info.java

Note the modifier transitive which follows the requires directive. This indicates that any other module which reads the com.rayfocus.services will also read the module com.rayfocus.domain . This is called implied readability.

Create a service class MessageService.java

Add the below contents to MessageService.java

Step 2: Module – com.rayfocus.domain

Create another module com.rayfocus.domain

Add the below contents to module-info.java

Create a domain class Message.java

Add the below contents to Message.java

Step 3: Modify com.rayfocus.app module

Two corrections needs to be done in the com.rayfocus.app module.

  • Add the com.rayfocus.services module as dependency.
  • Correct the Client.java class to use the MessageService.

Add the below contents to com.rayfocus.app/module-info.java

Add the below contents to Client.java

Compile multiple modules

Compile the multiple modules in the order as below.

Run the multiple module program

Execute the multiple module program using the below command.

Final Project structure tree

The multiple module project will have following structure.

Thoughts on implied readability

Well! Now the multiple modules are developed, compiled and executed. The implied readability works as expected. The module com.rayfocus.app is able to read the module com.rayfocus.domain which is declared as a transitive dependency in the module com.rayfocus.services.

Conclusion

Modules in Java gives a very strong encapsulation. From the example we have developed, we can see that in order to read a package in other module, the package has to be exported explicitly in module descriptor. Else the program won’t compile. Java 9 is going to be exciting to develop and will bring some challenges to migrate the existing applications.

Overall I feel it’s a welcome addition to Java! Share your opinions and comments as well.

Thank you!


Download source code : java9-modules-example.zip



Vignesh M

Java developer , AWS Certified Solutions Architect Associate and Cloud technology enthusiast. Currently working for Hexaware technologies. He believes that knowledge increases by sharing not by saving.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.