Java 9 Module System: Modularity and Encapsulation with Project Jigsaw Previews

Rethinking the JVM classpath. We explore Java modules, exports/requires directives, and modular runtime packaging.

VP
SHIVAM ITCS
·15 March 2017·10 min read·1 views

Technical Overview & Strategic Context

For two decades, Java applications relied on the classpath to resolve dependencies at runtime. However, the classpath model had limitations: it did not enforce encapsulation, allowing public classes to be accessed by any code, and it did not verify dependency completeness at startup, leading to ClassNotFoundException crashes at runtime. The upcoming release of Java 9 addresses this by introducing the Java Platform Module System (Project Jigsaw), which adds strong modularity and encapsulation to the JVM.

Architectural Principle: Enforce strong encapsulation at the module level. Declare public APIs explicitly using exports directives, and block access to internal implementation details.

Core Concepts & Architectural Blueprint

Project Jigsaw introduces the module-info.java configuration file, where developers define module boundaries. Modules must explicitly declare which packages they export to other modules and which modules they require as dependencies. The Java runtime verifies these dependencies at startup, preventing missing class errors in production. The module system also allows developers to create custom, lightweight JRE packages that bundle only the modules required by the application.

Performance & Capability Comparison

Java PackagingPre-Java 9 ClasspathJava 9 Modular JigsawOperational Benefit
EncapsulationPublic classes accessible from any packagePublic classes hidden unless exportedProtects internal APIs from access
Dependency VerificationDynamic loading (risk of ClassNotFoundException)Static validation during startupPrevents missing dependency crashes
JRE SizeMonolithic runtime environment (approx. 200MB+)Custom runtime using jlink tool (approx. 40MB)Reduces container image sizes

Implementation & Code Pattern

To configure a modular Java project under Java 9 guidelines, developers should follow these steps:

  • Create a module-info.java file in the source root directory.
  • Specify the module name and dependencies using requires statements.
  • Declare public APIs using exports statements to expose packages.
  • Package modular applications using the jlink tool to create custom runtimes.
javacode
// module-info.java module declaration in Java 9 (2017)
module in.shivamitcs.portal {
    // Require Java foundational core modules
    requires java.sql;
    requires java.logging;

    // Export public API packages to other modules
    exports in.shivamitcs.portal.api;
    exports in.shivamitcs.portal.dto;

    // Internal implementation packages remain hidden automatically
}

Operational Governance & Future Outlook

The Java 9 Module System (Project Jigsaw) resolved key limitations of the traditional classpath. Enforcing strong encapsulation and startup verification helps teams build reliable JVM applications.

VP
Vijay Paliwal
Founder, SHIVAM ITCS · 18+ years enterprise & AI engineering
MCA · Ex-HiveGPT USA · Ex-Social27 Seattle
Java 9 Module System: Modularity and Encapsulation with Project Jigsaw Previews | SHIVAM ITCS Blog | SHIVAM ITCS