Role Of Readability

When one module depends directly upon another in the module graph then code in the first module will be able to refer to types in the second module. We therefore say that the first module reads the second or, equivalently, that the second module is readable by the first. Thus, in the above graph, the com.foo.app module reads the com.foo.bar andjava.sql modules but not the org.baz.qux, java.xml, or java.loggingmodules. The java.logging module is readable by the java.sql module, but no others. (Every module, by definition, reads itself.)

The readability relationships defined in a module graph are the basis of reliable configuration: The module system ensures that every dependence is fulfilled by precisely one other module, that the module graph is acyclic, that every module reads at most one module defining a given package, and that modules defining identically-named packages do not interfere with each other.

Reliable configuration is not just more reliable; it can also be faster. When code in a module refers to a type in a package then that package is guaranteed to be defined either in that module or in precisely one of the modules read by that module. When looking for the definition of a specific type there is, therefore, no need to search for it in multiple modules or, worse, along the entire class path.

 

java-9-module-Readability

java-9-module-Readability

Example:

javadeveloperzone.base module

base-module

base-module

 

module-info.java

  • exports indicate that other modules can read that package. here other package can access all resources of com.javadeveloperzone.model package.
  • javadeveloperzone.base also contains com.javadeveloperzone.internal package but is will not be accessible out side of javadeveloperzone.base module.
  • exports is not keyword
module javadeveloperzone.base{
exports com.javadeveloperzone.model;            // do not forgot to add exports here.
}

Student.java

package com.javadeveloperzone.model;
public class Student{
 public int no;
 public String name;
 public Student(int no,String name){
 this.no=no;
 this.name=name;
 }
 public String getName(){
 return this.name;
 }
}

SecureStudent.java

  • SecureStudente.java only accessible current module because in module-info.java write for exporting <code>com.javadeveloperzone.model<code> package only not for java.developerzone.internal package.
package com.javadeveloperzone.internal;
public class StudentSecure{
  public int no;
  public String name;
  public StudentSecure(int no,String name){
    this.no=no;
    this.name=name;
  }
  public String getName(){
    return this.name;
  }
 }

 

javadeveloperzone.foo module

module-info.java

  • javadeveloperzone.foo is name of module
  • requires indicates javadeveloperzone.foo want to read javadeveloperzone.base module
  • Java 9 provides lots of modules any of module that want to read at that its compulsory add
    • requires [module_name]
  • java.base is default module which automatically added by compiler so no need to add java.base in module-info.java.
  • requires is not keyword
module javadeveloperzone.foo{
  requires javadeveloperzone.base;   // do not forgot to add require here.
}

 FooDemo.java

  • In FooDemo.java file accessing com.javadevloperzone.Student class which is available in javadeveloperzone.base module and javadeveloperzone.base module exporting com.javadeveloperzone package.
package com.javadeveloperzone.foo;
import com.javadeveloperzone.Student;
public class FooDemo{
  public static void main(String ... args){
    System.out.println("Foo modules is accessing javadeveloperzone.base module");
    Student student = new  Student(1,"FooStudent");
    System.out.println(student.getName());
  }
}

Create module jar for javadeveloperzone.base

echo "Compiling Class files of module"
javac -d output/classes base/com/javadeveloperzone/model/*.java
javac -d output/classes base/com/javadeveloperzone/internal/*.java
javac -d output/classes base/Module-info.java
echo "Creating javadeveloperzone.base module Jar"
mkdir mlib
jar -c -f mlib/javadeveloperzone.base.jar -C output/classes .

Create module jar for javadeveloperzone.foo

javac --module-path mlib -d outputsecond/classes foo/Module-info.java
javac --module-path mlib -d outputsecond/classes foo/com/javadeveloperzone/foo/*.java
echo "Creating javadeveloperzone.foo module Jar"
mkdir mlib
jar -c -f mlib/foo.jar -C outputsecond/classes .

Running javadeveloperzone.foo module

echo "Running second module..."
java --module-path mlib -m javadeveloperzone.foo/com.javadeveloperzone.foo.FooDemo
modules-output

modules-output

Let check modules dependency

jdeps -s  –module-path . foo.jar

 

Was this post helpful?

Leave a Reply

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