⬅ Previous Next ➡

Modules (Java 9+)

Java Modules (Java 9+): Module System, module-info.java, Creating/Using Modules & Advantages
  • Java Module System (introduced in Java 9) is used to organize large applications into modules.
  • A module is like a group of packages with a clear boundary: what it exports and what it requires.
  • Main file: module-info.java (module descriptor).

1) Introduction to Java Module System

  • Before modules, Java apps used classpath which could cause:
    • Dependency conflicts (same library multiple versions)
    • Hard to control access to internal packages
    • Large applications become difficult to manage
  • Modules solve this using explicit dependencies and strong encapsulation.

2) module-info.java (Module Descriptor)

  • Defines module name and rules.
  • Common keywords:
    • module : declares module
    • requires : dependency module
    • exports : packages visible to other modules
    • opens : allows reflection (used with frameworks)
    • provides ... with ... : service provider
    • uses : service usage
// module-info.java (basic)
module com.webtechio.app {
    requires java.base; // optional (java.base is default)
}

3) Creating and Using Modules (Example: math + app)

  • Create two modules:
    • com.webtechio.math (exports math package)
    • com.webtechio.app (requires math module and uses it)
// Folder Structure (example)
// src
//   com.webtechio.math
//     module-info.java
//     com/webtechio/math/Calc.java
//   com.webtechio.app
//     module-info.java
//     com/webtechio/app/Main.java
// src/com.webtechio.math/module-info.java
module com.webtechio.math {
    exports com.webtechio.math;
}
// src/com.webtechio.math/com/webtechio/math/Calc.java
package com.webtechio.math;

public class Calc {
    public int add(int a, int b) {
        return a + b;
    }
}
// src/com.webtechio.app/module-info.java
module com.webtechio.app {
    requires com.webtechio.math;
}
// src/com.webtechio.app/com/webtechio/app/Main.java
package com.webtechio.app;

import com.webtechio.math.Calc;

public class Main {
    public static void main(String[] args) {
        Calc c = new Calc();
        System.out.println("Sum: " + c.add(10, 20));
    }
}

4) Compiling and Running Modules (Command Line)

  • Compile modules using --module-source-path.
  • Run using --module-path and -m (module/class).
// Compile (creates output in out folder)
javac -d out --module-source-path src $(find src -name "*.java")

// Run module
java --module-path out -m com.webtechio.app/com.webtechio.app.Main

5) Advantages of Modular Programming

  • Strong Encapsulation: only exported packages are accessible.
  • Reliable Configuration: dependencies are declared using requires.
  • Better Maintainability: modules are easier to manage and update.
  • Improved Security: internal APIs can be hidden.
  • Faster Startup for large apps (only required modules loaded).
  • Easy Reuse: modules can be reused across projects.

6) Quick Notes

  • java.base is automatically available to all modules.
  • Use exports to make a package public to other modules.
  • Use requires to depend on another module.
  • Module name should look like a reversed domain style: com.company.module.
⬅ Previous Next ➡