Open In App

JPMS : Java Platform Module System

JPMS: JPMS i.e. Java Platform Module System is a new feature which is introduced in Java 9. With the help of the Java module system, we can package our Java application and our Java packages into Java modules. By the help of the Java module, we can specify which of the packages of the module should be visible to other Java modules. A Java module must also specify which other Java modules is requires to do its job. 
The Java Platform Module System is also sometimes referred to as Java Jigsaw or Project Jigsaw depending. Jigsaw was the internally used project name during development. Later Jigsaw changed name to Java Platform Module System. The main intent of developing JPMS is to make the JRE more modular i.e. have smaller jars which are optional and/or we can download/upgrade only the functionality as per need.
JPMS/Project Jigsaw is going to address few major problems:
 

  1. ClassPath/JAR Hell 
     
  2. Massive Monolithic JDK 
     
  3. Version conflicts 
     
  4. Security Problems 
     

Let’s see each one of them in detail.
 






class Geeksforgeeks {
    public static void main(String[] args)
    {
        System.out.println(
            "Hello, "
            + "Welcome to Geeksforgeeks!!!");
    }
}

To run the above code, we need few classes like String, System, Object etc. But we have to hold the entire JDK even if we are not using the entire jar of JDK not even 10% of it. With this approach to run even 1KB of file, we need to have minimum 60MB of rt.jar file. We can resolve this issue with JPMS. Jigsaw breaks up the JDK itself into many modules e.g. Java.sql contains the familiar SQL classes, Java.io contains the familiar IO classes etc. As per requirement, we can use appropriate module. No need to use the entire JDK.
 



Here we have 4 jar files inside classpath. Suppose JAR 4 requires a .class file with name xyz of JAR 3 and JAR 2 also contains the .class file with the name xyz. Now JVM will search for xyz class file from left to right and it will get the .class file named with xyz from JAR 2. Now the JVM will assign xyz.class file of JAR 2 inside JAR 4. But JAR 4 requires xyz.class file of JAR 3. Here we will get version conflict. With the help of JPMS, we can specify that we want a .class file of a particular JAR file. We have to specify inside module-info.Java like below:
 




module module4
{
    requires module3;
}

com.geeksforgeeks.demo.api.geeks
com.geeksforgeeks.demo.impl.geeksImpl

Both packages are public in nature. At some point, we might decide that our team should use geeks package and not use geeksImpl directly. However, there is no way to enforce that on the classpath. In JPMS/Jigsaw, a module contains a module-info.Java file which allows us to explicitly state what is public to other modules. 
 




// com.geeksforgeeks.demo.api.geeks is accessible,
// but com.geeksforgeeks.demo.impl.geeksImpl is not
 
module com.geeksforgeeks.demo
{
    exports com.geeksforgeeks.demo.impl;
}


Article Tags :