Java 9 Module System

Java Module System is a major change in Java 9 version. Java added this feature to collect Java packages and code into a single unit called module.

In earlier versions of Java, there was no concept of module to create modular Java applications, that why size of application increased and difficult to move around. Even JDK itself was too heavy in size, in Java 8, rt.jar file size is around 64MB.

To deal with situation, Java 9 restructured JDK into set of modules so that we can use only required module for our project.

Apart from JDK, Java also allows us to create our own modules so that we can develop module based application.

The module system includes various tools and options that are given below.

  • Includes various options to the Java tools javac, jlink and java where we can specify module paths that locates to the location of module.
  • Modular JAR file is introduced. This JAR contains module-info.class file in its root folder.
  • JMOD format is introduced, which is a packaging format similar to JAR except it can include native code and configuration files.
  • The JDK and JRE both are reconstructed to accommodate modules. It improves performance, security and maintainability.
  • Java defines a new URI scheme for naming modules, classes and resources.

Java 9 Modularized JDK

Java 9 Module System 1

Java 9 Module

Module is a collection of Java programs or softwares. To describe a module, a Java file module-info.java is required. This file also known as module descriptor and defines the following

  • Module name
  • What does it export
  • What does it require
Java 9 Module

Module Name

It is a name of module and should follow the reverse-domain-pattern. Like we name packages, e.g. com.w3cschoool.

How to create Java module

Creating Java module required the following steps.

  • Create a directory structure
  • Create a module declarator
  • Java source code

Create a Directory Structure

To create module, it is recommended to follow given directory structure, it is same as reverse-domain-pattern, we do to create packages / project-structure in Java.

Note: The name of the directory containing a module's sources should be equal to the name of the module, e.g. com.w3cschoool.

Java9 Module System 2

Create a file module-info.java, inside this file, declare a module by using module identifier and provide module name same as the directory name that contains it. In our case, our directory name is com.w3cschoool.

Leave module body empty, if it does not has any module dependency. Save this file inside src/com.w3cschoool with module-info.java name.

Java Source Code

Now, create a Java file to compile and execute module. In our example, we have a Hello.java file that contains the following code.

Save this file inside src/com.w3cschoool/com/w3cschoool/ with Hello.java name.

Compile Java Module

To compile the module use the following command.

After compiling, it will create a new directory that contains the following structure.

Java9 Module System 3

Now, we have a compiled module that can be just run.

Run Module

To run the compiled module, use the following command.

Output:

Hello from the Java module

Well, we have successfully created, compiled and executed Java module.

Look inside compiled Module Descriptor

To see the compiled module descriptor use the following command.

This command will show the following code to the console.

See, we created an empty module but it contains a java.base module. Why? Because all Java modules are linked to java.base module and it is default module.


Next TopicControl Panel




Contact US

Email:[email protected]

Module System
10/30