Toll Free:

1800 889 7020

Java Platform Module System (JPMS) in Java 9 and Beyond

The Java platform module system was introduced in Java 9 and was designed to make the platform more secure and scalable. It also allows developers to define modules for better encapsulation and dependency management. It comes up with a major change in the Java 9 version. The work is to collect the Java packages and code into a single unit called a module. To use only the required modules for our project, Java restructured JDK into a set of modules. Besides JDK, it also allows us to create our modules so that developers can create module-based applications easily. Module system contains various tools given below:
  • Introduction of Module JAR file, which contains a module-info class file in the root folder.
  • Introduction of JMOD format, a packaging format similar to JAR, which contains native code and configuration files.
  • For the improvement of other factors such as performance, security JDK and JRE both are reconstructed to accommodate modules.

1. What is a module?

It is a named grouping of code like classes and interfaces and other resources like images, files, etc. Every module declares what it exports and what it depends on (requires). In other words, a module is a collection of Java outsourcing programs or software. module-info.java file is required to describe a module. This file is also called a descriptor, and it defines the following:
  • Module name
  • What does a module exports
  • What does a module require

2. Creating a Java Module

We can create a module by following some simple steps.
  • Create a Directory Structure: We have to follow a reverse domain pattern similar to how we create packages in Java.
  • Create a Module Declarator: Create a file named module-info.java as a module declarator, and inside the file create a module using the module identifier. After this, use the module name the same as a directory name, and if the file has no dependency, leave it empty.
  • Java source code: Here we can create the source code file with the nameMain.java and save it.

2.1 Example

2.1.1 Creating a Directory Structure

xyz
│
├── src
│   └── module-info.java
│   └── com
│        └── xyz
└── out (generated by the compiler)

2.2.2 Creating a Module Declarator

Here we need to declare the module name, like here we select ‘xyz’.
module com.xyz {
  //body
}

2.2.3 Creating a Source Code File

Here we can create a source code file named Main.java and save it.
//Creating a source code file in Java
class Main {
  public static void main(String args[]) {
    System.out.println("Hello Visitors...!!");
  }
}

2.2.4 Output

Here we get the output generated by the compiler:
Hello Visitors...!!

3. Modules vs. JARs

Traditionally, JARs are used to package Java code, as they don’t provide the same level of control over dependencies and visibility. The module is a much more advanced concept, and a JAR can contain a module by including themodule-info.java file.

4. Features of JPMS

  • Strong Dependency Management: Some modules can be dependent on other modules as well. This can be part of the module definition itself. Modules explicitly declare the other modules, and then they depend on the usage of the require keyword. This ensures that the module system can manage dependencies at compile and runtime.
  • Strong Encapsulation: Implementation details can be hidden by modules; only the explicitly declared public packages by modules are accessible to other modules. The main purpose is that the encapsulated code may change freely without affecting the module’s users.
  • Performance enhancement: Allows better optimization at runtime because of its strong and more explicit structure.

5. Modular JDK

JDK (Java Development Kit) in terms of Java Platform Module System refers to the JDK being divided into a set of modules. Depending on the needs of the application each of which can be included or excluded. It was a monolithic system before JAVA 9, all the components are bundled together. The modular JDK breaks this monolith into separate modules. Each module is a self-contained package that contains related classes and resources. javac command is used by the developers with module options to compile modular applications and the java command with module option to run module applications.

5.1 Example

5.1.1 Create a module-info.java file

This file is created to define the module and its dependencies. Make sure to create this file in the root of your src directory.
module com.example.myModule {
  // Mention all the exported packages
  exports com.example.myModule.services;

  // Mention all the required modules here.
  requires java.sql;
}

5.1.2 Code Breakdown

  • Our very first step is to declare the module named com.example.myModule
  • .
  • Secondly, we make the com.example.myModule.service package available to other modules also.
  • At last we specify the module, which depends on the Java.sql module.

5.1.3 Structure your project

It helps in managing the module’s source code and resources. Source files are organized into directories that mirror the package structure with the module-info file.
myModule
│
├── src
│   └── module-info.java
│   └── com
│       └── example
│           └── myModule
│               └── service
│                   └── MyService.java
└── out (generated by the compiler)

5.1.4 Implementation of classes

Here, Create classes within the packages you declared in your module.
package com.example.myModule.services;

 public class MyService {
   public void printMessage() {
     System.out.println("Hello Visiters...!!");
   }
 }

5.1.5 Compilation of Module

  • In order to compile the module, we use the javac command.
  • The command compiles all the .java file in the src directory and then places the compiled classes in the out directory, which results in preserving the module structure.
javac -d out --module-source-path src $(find src -name"*.java")
This is a very basic example of setting up and using the Java Platform Module System. We can build more complex modules by using advanced dependencies and services as your application grows.

6. Conclusion

Initially, in the development phase, the Java Platform Module System was also named Project Jigsaw. It makes the task easier for the developers to organize large applications and libraries by increasing the structure and level of security of the platform. It is very useful in enhancing the performance of any application and for the decomposition of the platform for smaller devices. The main idea behind this is to enable the collection of related packages that are visible to the module while hiding the elements from external consumers on the module. It permits Java developers India to efficiently structure their applications by organizing code into modules.

Cleveland

Scroll to Top