Introduction about Java Module System

Java Module System was introduced by Java from Java 9 to solve some problems of Java that if you notice you will see how its necessary?

  • NoClassDefFoundError: This error occurs while our Java program is running. The JVM cannot find the class needed to execute a certain task. Although we now have tools to build and fetch all the necessary dependencies to run applications like Maven, Gradle, … but do you agree with Khanh: sometime we also get this error.
  • Any class or method defined with public or protected access modifier in Java libraries that is located in the classpath of the application’s project, we can access them. There is no way for these Java libraries to hide them, only for the classes of packages located in the library to be accessible.
  • And many more other problems…

Java Module System was introduced to solve the above problems:

  • It makes it possible for us to manage which modules will be used in our code, which will be included during our module build, by using the ‘requires’ module directive.
  • We can also decide who and which packages can use our code, using the keyword module directive ‘exports’ ….

In this tutorial, I will give you an overview of the Java Module System with a small example of how to create a new Java module!


Overview about Java Module

First, please install JDK from Java 9 onwards.

Open the command line and then enter the command line:

to see all the modules provided by default by Java.

Result:

Here, Java divides into two types of modules, which are standard modules and non-standard modules. Standard modules are modules that implement the Java SE specification with module names starting with java.* and non-standard modules are modules included in the Java Development Kit that start with jdk.*. You can clearly see this in the results after I execute the “java –list-modules” statement above.

Each module name will include the module name and Java version telling us which Java version the module belongs to. As you can see, I’m using Java 15 and in the module names, as you can see, they end with @15.

To view information about a certain module, you can use the following command:

For example, to view the information of the java.logging module, I would enter the following command:

Result:

Introduction about Java Module System

The information of a module will start with the module name, and followed by the module directives that I mentioned above such as exports, requires, provides, … as you can see in the image above. All this information will be defined in a file called module-info.java.

Each module directive has the effect of defining how the module will be built and used, for example, the ‘exports’ directive helps us define a certain package in this module is accessed by all packages or other certain packages, or the ‘provides’ directive has the purpose of specifying what this module is doing: implement a certain interface for other classes to use, … We will learn more about each module directive later!

In the output of the above example, you can see that the java.logging module is using a dependency called the java.base module. To see all the dependencies that a module is using, you can also use the command with the jdeps tool as follows:

Or you can abbreviate –module with -m as follows:

For example, to see the dependencies that the java.logging module is using, I would run the following command:

Result:

Introduction about Java Module System


Example

Now, I will show you how to create a new basic module!

First, I will create a new Java project as an example.

I will use Spring Tool Suite to do this.

During the process of creating a new project, you will see that Spring Tool Suite supports us to create a new module-info.java file.

Introduction about Java Module System

and:

Introduction about Java Module System

As I said above, this is the file that defines the information of a Java Module.

Result:

Introduction about Java Module System

The content of the file module-info.java now you can see as follows:

Now I will add a class with sayHello() method, return the word “Hello World” as follows:

And export the package of this class in the module-info.java file using ‘exports’ directive so that other modules can use the Example class as follows:

Now, I will create another Java module project:

Introduction about Java Module System

I will declare the module huongdanjava.module.example.test using the module huongdanjava.module.example by right-clicking on the project huongdanjava.module.example.test and select Build Path, then select Configure Build Path… In the Java Build Path window, I will select module huongdanjava.module.example to use as follows:

Introduction about Java Module System

This module huongdanjava.module.example.test will declare ‘requires’ directive to the package huongdanjava.module.example in the file module-info.java to use the Example class of the huongdanjava.module.example module:

and print the words “Hello World” when running the program as follows:

The output when running this program is as follows:

Introduction about Java Module System

If now in the module-info.java file of the huongdanjava.module.example module, I delete the line

you will see Spring Tool Suite reporting the Application class error right away as follows:

Introduction about Java Module System

That’s because at this point, we don’t allow any classes in other modules to use the huongdanjava.module.example package of the huongdanjava.module.example module anymore!

We will learn more about the directives module in Java Module in the incoming tutorials of Huong Dan Java.

Add Comment