This article contains Java 9 module example with detail explanation. Java 9 provide module based development. Main focus of java 9 is architecture level changes where as java 8 was on way of coding. Most important and useful feature of java 9 is module based development. Here is Java 9 module example:

Here is inbuilt modules structures which are in Java 9 JDK:

Java 9 module System Structure

Java 9 module System Structure

What is module in Java 9?

module is collection more java files , jar files, resource files and configuration files. There some rules of module declaration. Those are as under:

  • module has unique name
  • module-info.java file must be at root location inside module see above folder structure.
  • requires : What to need
  • exports : What to provide

NOTE: requires and exports are not keywords

Example:

Module Structure:

Java 9 module example Structure

Java 9 module example Structure

module-info.java

  • module-info.java file must be at root location inside module see above folder structure.
  • class name must be module-info but there are no strict rules for case sensitivity. We can also like MODULE-INFO.JAVA or Module-Info.java ect…
  • javadeveloperzone.base is name of module.
module javadeveloperzone.base{
  
}

Student.java

  • Student.java class is like normal class as like other no need of any other changes in this. its available inside com.javadeveloperzone package.
package com.javadeveloperzone;
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;
  }
}

Demo.java

  • Demo.java is another class in com.javadeveloperzone package.
package com.javadeveloperzone;
import com.javadeveloperzone.Student;
public class Demo{
 public static void main(String ... args){
 System.out.println("Niceee... weldone... welcome to your first module program..");
 Student student= new Student(1,"JavaDeveloperZone");
 System.out.println(student.getName());
 }
}

Commands to run module

  • Using javac just compile all class in package as well as module-info.java file.
  • using jar command create jar of module which contains com.javadeveloperzone package as well as module.info.class at root of structure.
  • using java command run module
    • –module-path indicate that module path
    • -m [module_name]/[main_class_of_module]
  • module-path must have all module with unique name, If modules have same name in same module-path than java will throw exception when try to run module.
echo "Compiling Class files of module"
javac -d output/classes base/com/javadeveloperzone/*.java
javac -d output/classes base/Module-info.java
echo "Creating Jar of module"
jar -c -f output/mlib/javadeveloperzone.base.jar -C output/classes .

echo "Running javadeveloperzone.base module..."
java --module-path output/mlib -m javadeveloperzone.base/com.javadeveloperzone.Demo

 

Was this post helpful?

Leave a Reply

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