Java 9 introduce module system but in modules system mainly we need to take care about accessibility of data, In another word we need to take care about which data we are exporting to other modules who will use our modules. There is so many other important frameworks are there like hibernate, JSON converters which runtime access our class using reflection.

The initial level of Java 9 development there are no such concepts like open modules after requirement other frameworks open modules concept need to add in Java 9 module system.  Let’s try to understand what is a requirement of open modules and in which condition it will help us.

What are open modules in Java 9?

Open module is module which export all packages at run time.

When we declare any module or package as open at that time those all packages will be accessible by other modules at runtime using like refection. Here note that those modules or package only accessible at runtime not at compile time. if anyone writes export explicitly at that time those packages can be accessed at compile time

Based on above definition we have some question like what is the actual need to allowed to access package at runtime but compile times. Ans is here,

Destination module can access only those packages which are exported by sources package’s public class other class cannot access by destination module. But in Java, some of the frameworks like hibernate or JSON parser API need to access other class of user for a model or convert data to JSON in that case user need to export those packages for hibernate or JSON parser framework and it was not possible to manage those type of stuff. So here the concept of open modules comes in pictures.

There are two ways to declare open data from modules

1. Declare open module:

open indicates that all packages inside this modules can accessible by the runtime. If we want to be allowed some package accessible at compile time at that we need to add export statement explicitly.

open module javadeveloper.base {
 
}

2. Declare opens package

opens indicate that all class inside defied packages will be accessible at runtime.

module javadeveloper.base {
    opens com.model;
}
NOTE: Open module doesn't mean at it will only allowed to access package at run time but If any package declare as export explicitly 
in module declaration than those package will be accessible at compile time by other modules.

Example of open module

Module 1: javadeveloper.base

module-info.java

open module javadeveloper.base {

}

Student.java

package com.javadeveloperzone.model;

/**
 * Created by JavaDeveloperZone on 13-05-2017.
 */
public class Student {
    private int no;
    private String name;
    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

 Module 2: javadeveloperzone.foo

/**
 * Created by JavaDeveloperZone on 22-04-2017.
 */
module javadeveloper.foo {
 requires javadeveloper.base;
}

Demo.java

In below example, I am trying to access com.javadeveloperzone.model.Student runtime using reflection we can access Student class because javadevloper.base module is declared as open. But we can not access Student Object at compile time because the open module cannot allow those package at compile time, To allowed compile time need to add explicitly export statement in module declaration.

package com.foo;

public class Demo {
    public static void main(String[] args) {
        // com.javadeveloperzone.model.Student student=new com.javadeveloperzone.model.Student();  we can not access at compile time but we can access at run time.
        System.out.println("This is foo module Main method");
        try {
            Object o = ClassLoader.getSystemClassLoader().loadClass("com.javadeveloperzone.model.Student")
                                  .newInstance();       // we can create run time object because javadeveloper.base module declare as open
            System.out.println(o.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Build Modules:

"C:\Program Files\Java\jdk-9\bin\java" -Dmaven.multiModuleProjectDirectory=F:\extrawork\java9-demo\module-multiple-modules -Xmx2g -XX:ReservedCodeCacheSize=512m "-Dmaven.home=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2017.1\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2017.1\plugins\maven\lib\maven3\bin\m2.conf" "-javaagent:C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2017.1\lib\idea_rt.jar=58738:C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2017.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2017.1\plugins\maven\lib\maven3\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version=2017.1 install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] com.javadeveloperzone
[INFO] javadeveloper.base
[INFO] foo
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building com.javadeveloperzone 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ com.javadeveloperzone ---
[INFO] Installing F:\extrawork\java9-demo\module-multiple-modules\pom.xml to C:\Users\Lenovo\.m2\repository\module-demo\com.javadeveloperzone\1.0-SNAPSHOT\com.javadeveloperzone-1.0-SNAPSHOT.pom
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building javadeveloper.base 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ javadeveloper.base ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.6.0:compile (default-compile) @ javadeveloper.base ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ javadeveloper.base ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\extrawork\java9-demo\module-multiple-modules\base\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.6.0:testCompile (default-testCompile) @ javadeveloper.base ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ javadeveloper.base ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ javadeveloper.base ---
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ javadeveloper.base ---
[INFO] Installing F:\extrawork\java9-demo\module-multiple-modules\base\target\javadeveloper.base-1.0-SNAPSHOT.jar to C:\Users\Lenovo\.m2\repository\module-demo\javadeveloper.base\1.0-SNAPSHOT\javadeveloper.base-1.0-SNAPSHOT.jar
[INFO] Installing F:\extrawork\java9-demo\module-multiple-modules\base\pom.xml to C:\Users\Lenovo\.m2\repository\module-demo\javadeveloper.base\1.0-SNAPSHOT\javadeveloper.base-1.0-SNAPSHOT.pom
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building foo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ foo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.6.0:compile (default-compile) @ foo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to F:\extrawork\java9-demo\module-multiple-modules\foo\target\classes
[WARNING] /F:/extrawork/java9-demo/module-multiple-modules/foo/src/main/java/com/foo/Demo.java:[9,35] newInstance() in java.lang.Class has been deprecated
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ foo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\extrawork\java9-demo\module-multiple-modules\foo\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.6.0:testCompile (default-testCompile) @ foo ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ foo ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ foo ---
[INFO] Building jar: F:\extrawork\java9-demo\module-multiple-modules\foo\target\foo-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ foo ---
[INFO] Installing F:\extrawork\java9-demo\module-multiple-modules\foo\target\foo-1.0-SNAPSHOT.jar to C:\Users\Lenovo\.m2\repository\module-demo\foo\1.0-SNAPSHOT\foo-1.0-SNAPSHOT.jar
[INFO] Installing F:\extrawork\java9-demo\module-multiple-modules\foo\pom.xml to C:\Users\Lenovo\.m2\repository\module-demo\foo\1.0-SNAPSHOT\foo-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] com.javadeveloperzone .............................. SUCCESS [  0.489 s]
[INFO] javadeveloper.base ................................. SUCCESS [  1.875 s]
[INFO] foo ................................................ SUCCESS [  1.117 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.638 s
[INFO] Finished at: 2017-05-14T00:12:56+05:30
[INFO] Final Memory: 14M/49M
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

Output:

This is foo module Main method, here we will access Student class at tun time.
0,null

 

References:

http://openjdk.java.net/projects/jigsaw/quick-start

 

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

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