Java 9 Modules

java

By
Narendran Solai Sridharan

Key Items

  1. Goals of JPMS

  2. Problems in Module Systems

  3. Modularity Maturity Model

  4. Modular JDK

  5. Module in Java

  6. Working of Module System

  7. Layers

  8. Migration to Java 9

Goals of JPMS

  • Strong Encapsulation

  • Reliable Configuration

  • Scalable Java Platform

  • Greater Platform Integrity

  • Improved Performance

Problems in Module Systems

OSGI, JBoss Modules, Spring Dynamic Modules are few Modular Development Systems.

We face following problems like,

  1. JAR hell / Classpath hell

    1. shadowing

    2. version conflict

  2. Issue with Unexpressible & Transitive Dependencies

  3. Complex Class Loading

Module Maturity Models

Current System

mmm 1

JPMS

mmm 2

Modular JDK

platform modules

Module in Java

Everything is a module in Java 9.

Demo Time!!!

Notable Restrictions

Cyclic Dependencies

No Cycles in Dependency is allowed in JPMS

cyclic dependency

Split Packages

No two modules can have same package either it is exported or not.

split packages

No Multi Module Support

We cannot have more than one module in an module artifact, either it is a jar or jmod file.

Working of Module System

Module Path & Module Resolution

All modules should be placed in the Module path. Entire classpath is considered as one single "Unnamed" Module.

Types of Module Resolution

  • Compile Time Resolution

  • Run Time Resolution

Types of Modules

module types
  • Automatic Modules

  • Unnamed Modules

  • Open Modules

Other kind of Classification

  • Aggregator Module

  • Incubator Modules

Impact on Reflection

impact on refections

Automatic Module

Upgrade an existing Module

Implicit Dependency

module org.wildcraft.eaz {
    exports org.wildcraft.eaz;
}

//daz does not just exports it,
//It also exports eaz
module org.wildcraft.daz {
    exports org.wildcraft.daz;
    requires transitive org.wildcraft.eaz;
}

//By depending on daz,
//caz implicity depends on eaz
module org.wildcraft.caz {
    requires org.wildcraft.daz;
}

Optional Dependency

//gaz requires haz only at compile time
//It also depends on iaz implicitly
module org.wildcraft.gaz {
    requires static org.wildcraft.haz;
}

//haz depends on iaz only at compile time
//it also export iaz
module org.wildcraft.haz {
    exports org.wildcraft.haz;
    requires transitive static org.wildcraft.iaz;
}

module org.wildcraft.iaz {
    exports org.wildcraft.iaz;
}

Versioning Modules

Testing Modules

Layers

Layers holds various modules.

layer n classloader

Configuration and multi layers

In each layer one or more classloaders can be created based on Configuration.

multilayer n multiparent

Modularity Rail Road Diagram

module configuration