Jump to content

Java Platform Module System

From Alfawiki
Java Platform Module System
Company typeSoftware specification
IndustrySoftware development
FoundedSeptember 21, 2017; 9 years ago (2017-09-21)
ParentJava Platform, Standard Edition

The Java Platform Module System establishes a standardized format for packaging collections of Java code alongside their related resources. It introduces a dedicated repository for these collections, known as modules, and specifies mechanisms for their discovery, loading, and integrity validation. This system resolves challenges inherent in the traditional JAR format, particularly problems such as JAR Hell, which lead to conflicts in classpath and class loading.

Originally proposed through the Java Community Process as JSR 277, the module system was intended for inclusion in Java 7 but was postponed. Subsequently, Project Jigsaw was initiated to modularize the JDK, replacing JSR 277 with JSR 376 (the Java Platform Module System).

Project Jigsaw aimed for release with Java 7 (2011), then delayed to Java 8 (2014) under an alternative plan, and ultimately debuted with Java 9 in 2017. Java 9 officially introduced the Java Module System on September 21, 2017.

Architecture

The Java Module System implemented in Java 9 integrates several essential JEPs and JSRs:

  • JEP 200: Establish a modular framework for the JDK
  • JEP 201: Modularize the JDK source code and enforce module boundaries during build processes
  • JEP 220: Reorganize JDK/JRE runtime images to support modules, enhancing performance, security, and maintainability
  • JEP 261: Implement the Java Platform Module System
  • JEP 282: Develop a utility to assemble and optimize modules into customized runtime images
  • JSR 376: Specification of the Java Platform Module System

Additional enhancements in JDK 9 facilitate the shift toward modular architecture:

  • JEP 238: Multi-Release JAR Files enable embedding multiple Java-release-specific class versions within a single archive
  • JEP 253: Prepare JavaFX UI Controls & CSS APIs for modularization by defining explicit public APIs
  • JEP 260: Encapsulate most internal APIs by default while maintaining access to critical ones until suitable alternatives are available
  • JEP 275: Support Modular Java Application Packaging by allowing packaging of modules with their dependencies

Properties of modules

Modules organize code by explicitly declaring both dependencies and exported packages, contrasting with traditional Jar files. This explicitness improves code reliability and simplifies dependency management in complex applications.

Each module includes a declaration file named , located at its source root. The JDK enforces dependency correctness and module interactions at compile-time and runtime.

For instance, a module declaration like this: <syntaxhighlight lang="cpp"> module com.foo.bar {

   requires com.foo.baz;
   exports com.foo.bar.alpha;
   exports com.foo.bar.beta;

} </syntaxhighlight> indicates that com.foo.bar depends on com.foo.baz and exposes the packages com.foo.bar.alpha and com.foo.bar.beta. Only public members within these exported packages are accessible to dependent modules; private members remain inaccessible even through reflection. Between Java versions 9 and 16, enforcement of 'illegal access' was influenced by command-line options.

The modularization of the JDK itself occurred in Java 9, with the majority of the standard library exported via the java.base module.

Starting from Java 25, modules can be imported wholesale using import module, which automatically imports all exported packages. For example: <syntaxhighlight lang="Java" inline>import module java.sql;</syntaxhighlight> equates to importing all relevant packages from java.sql, javax.sql, and associated modules.

Similarly, <syntaxhighlight lang="Java" inline>import module java.base;</syntaxhighlight> imports every one of the 54 packages within java.base, reducing manual import statements: <syntaxhighlight lang="Java"> import module java.base;

/**

* Using 'import module java.base' eliminates manual imports of numerous classes,
* including:
* java.text.MessageFormat
* java.util.Date
* java.util.List
* java.util.concurrent.ThreadLocalRandom
*/

public class Example {

   public static void main(String[] args) {
       List<String> colours = List.of("Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet");
       IO.println(MessageFormat.format("My favourite colour is {0} and today is {1,date,long}",
           colours.get(ThreadLocalRandom.current().nextInt(colours.size())),
           new Date()
       ));
   }

} </syntaxhighlight>

Modules employ specific keywords for their declarations:

  • exports: makes packages accessible to other modules
  • module: defines a module
  • open: permits all classes within a package to be accessible via reflection
  • opens: opens a designated package for reflective access by other modules
  • provides: declares an implementation of a service interface
  • requires: specifies dependencies on other modules
  • to: limits reflective access to particular modules
  • transitive: exposes dependencies of required modules to downstream modules
  • uses: indicates consumption of services provided by other modules
  • with: identifies which service implementation is provided

Core modules

Modules prefixed with java.* are part of the Java Platform, Standard Edition, whereas those starting with jdk.* belong to the Java Development Kit.

<syntaxhighlight lang="text" class="" style="" inline="1">java.base</syntaxhighlight> Fundamental APIs that serve as the cornerstone of the Java SE Platform; these are automatically required by every module.
<syntaxhighlight lang="text" class="" style="" inline="1">java.compiler</syntaxhighlight> APIs related to language modeling, processing of Java annotations, and the Java compiler itself.
<syntaxhighlight lang="text" class="" style="" inline="1">java.datatransfer</syntaxhighlight> Interfaces facilitating data exchange both between and within applications.
<syntaxhighlight lang="text" class="" style="" inline="1">java.desktop</syntaxhighlight> Contains AWT and Swing UI toolkits, along with APIs for accessibility, audio, imaging, printing, and JavaBeans.
<syntaxhighlight lang="text" class="" style="" inline="1">java.instrument</syntaxhighlight> Provides services for instrumentation of programs running on the Java Virtual Machine (JVM).
<syntaxhighlight lang="text" class="" style="" inline="1">java.logging</syntaxhighlight> API designed to record logging events within Java programs.
<syntaxhighlight lang="text" class="" style="" inline="1">java.management</syntaxhighlight> APIs supporting the Java Management Extensions (JMX) framework.
<syntaxhighlight lang="text" class="" style="" inline="1">java.management.rmi</syntaxhighlight> RMI connector implementation for the JMX Remote API.
<syntaxhighlight lang="text" class="" style="" inline="1">java.naming</syntaxhighlight> API offering services for the Java Naming and Directory Interface (JNDI).
<syntaxhighlight lang="text" class="" style="" inline="1">java.net.http</syntaxhighlight> API enabling HTTP client capabilities and support for WebSocket communication.
<syntaxhighlight lang="text" class="" style="" inline="1">java.prefs</syntaxhighlight> API dedicated to handling user preference data.
<syntaxhighlight lang="text" class="" style="" inline="1">java.rmi</syntaxhighlight> Remote Method Invocation (RMI) API that facilitates communication between JVM instances.
<syntaxhighlight lang="text" class="" style="" inline="1">java.scripting</syntaxhighlight> API integrating various scripting engines into Java applications.
<syntaxhighlight lang="text" class="" style="" inline="1">java.se</syntaxhighlight> Defines the standard API for the Java SE Platform.
<syntaxhighlight lang="text" class="" style="" inline="1">java.security.jgss</syntaxhighlight> Java binding implementation of the IETF Generic Security Services API (GSS-API).
<syntaxhighlight lang="text" class="" style="" inline="1">java.security.sasl</syntaxhighlight> Support for the Simple Authentication and Security Layer (SASL) protocol.
<syntaxhighlight lang="text" class="" style="" inline="1">java.smartcardio</syntaxhighlight> API designed for interaction with smart card hardware devices.
<syntaxhighlight lang="text" class="" style="" inline="1">java.sql</syntaxhighlight> Java Database Connectivity (JDBC) API to facilitate database interactions.
<syntaxhighlight lang="text" class="" style="" inline="1">java.sql.rowset</syntaxhighlight> JDBC RowSet API managing tabular data structures in Java applications.
<syntaxhighlight lang="text" class="" style="" inline="1">java.transaction.xa</syntaxhighlight> API handling distributed transaction management within JDBC contexts.
<syntaxhighlight lang="text" class="" style="" inline="1">java.xml</syntaxhighlight> Java API for XML Processing (JAXP), including tools for parsing and transforming XML documents.
<syntaxhighlight lang="text" class="" style="" inline="1">java.xml.crypto</syntaxhighlight> API supporting cryptographic operations on XML content.
<syntaxhighlight lang="text" class="" style="" inline="1">jdk.accessibility</syntaxhighlight> Utility classes that assist Assistive Technologies within the JDK environment.
<syntaxhighlight lang="text" class="" style="" inline="1">jdk.attach</syntaxhighlight> Attach API enabling connections to live JVM instances.
<syntaxhighlight lang="text" class="" style="" inline="1">jdk.charsets</syntaxhighlight> Additional character sets beyond those in java.base, including double-byte and IBM-specific encodings.
<syntaxhighlight lang="text" class="" style="" inline="1">jdk.compiler</syntaxhighlight> Internal implementation details of the Java compiler and javac toolchain.
<syntaxhighlight lang="text" class="" style="" inline="1">jdk.crypto.cryptoki</syntaxhighlight> SunPKCS11 security provider facilitating cryptographic functions.
<syntaxhighlight lang="text" class="" style="" inline="1">jdk.dynalink</syntaxhighlight> API allowing dynamic linking of high-level object operations at runtime.
<syntaxhighlight lang="text" class="" style="" inline="1">jdk.editpad</syntaxhighlight> Service used by jdk.jshell to support code evaluation editing capabilities.
<syntaxhighlight lang="text" class="" style="" inline="1">jdk.hotspot.agent</syntaxhighlight> Implementation of the HotSpot Serviceability Agent for JVM diagnostics.
<syntaxhighlight lang="text" class="" style="" inline="1">jdk.httpserver</syntaxhighlight> JDK-specific HTTP server API along with the jwebserver tool for straightforward HTTP server deployment.
<syntaxhighlight lang="text" class="" style="" inline="1">jdk.incubator.vector</syntaxhighlight> API expressing computations that compile into SIMD instructions at runtime (e.g., AVX, NEON).
<syntaxhighlight lang="text" class="" style="" inline="1">jdk.jartool</syntaxhighlight> Tools such as jar and jarsigner for managing Java Archive (JAR) files.
<syntaxhighlight lang="text" class="" style="" inline="1">jdk.javadoc</syntaxhighlight> Implementation of system documentation tools including the command-line javadoc utility.
<syntaxhighlight lang="text" class="" style="" inline="1">jdk.jcmd</syntaxhighlight> Diagnostic utilities targeting JVM processes, including jcmd, jps, and jstat commands.
<syntaxhighlight lang="text" class="" style="" inline="1">jdk.jconsole</syntaxhighlight> Graphical management and monitoring tool based on JMX for Java applications.
<syntaxhighlight lang="text" class="" style="" inline="1">jdk.jdeps</syntaxhighlight> Tools analyzing dependencies in Java programs and libraries, such as jdeps and javap utilities.
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.jdi") </syntaxhighlight> getCode("Java Debug Interface (JDI) facilitating application debugging.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.jdwp.agent") </syntaxhighlight> getCode("Agent implementation of Java Debug Wire Protocol (JDWP) supporting remote debugging.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.jfr") </syntaxhighlight> getCode("Defines APIs for JDK Flight Recorder (JFR), which collects performance and diagnostic data.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.jlink") </syntaxhighlight> getCode("Toolset including 'jlink' to assemble custom runtime images and manage JMOD/JDK container files.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.jpackage") </syntaxhighlight> getCode("Packaging tool ('jpackage') creating platform-specific application installers.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.jshell") </syntaxhighlight> getCode("Interactive REPL tool ('jshell') for evaluating Java code snippets.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.jsobject") </syntaxhighlight> getCode("API integrating JavaScript objects within Java applications.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.jstatd") </syntaxhighlight> getCode("Remote JVM statistics monitoring tool ('jstatd').")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.localedata") </syntaxhighlight> getCode("Support for locale-specific data extending beyond US-centric locales.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.management") </syntaxhighlight> getCode("JDK-specific interfaces designed for JVM monitoring and management.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.management.agent") </syntaxhighlight> getCode("JMX-based management agent facilitating JVM control.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.management.jfr") </syntaxhighlight> getCode("Management interface dedicated to JDK Flight Recorder (JFR).")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.naming.dns") </syntaxhighlight> getCode("DNS provider implementing Java Naming services resolving domain names.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.naming.rmi") </syntaxhighlight> getCode("RMI-based Java Naming provider supporting remote method invocation.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.net") </syntaxhighlight> getCode("Network programming APIs specific to the JDK.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.nio.mapmode") </syntaxhighlight> getCode("File mapping modes available within the JDK environment.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.sctp") </syntaxhighlight> getCode("API supporting Stream Control Transmission Protocol (SCTP).")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.security.auth") </syntaxhighlight> getCode("Authentication modules and security interfaces under javax.security.auth package.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.security.jgss") </syntaxhighlight> getCode("Extensions to GSS-API and implementation of SASL GSSAPI mechanisms.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.xml.dom") </syntaxhighlight> getCode("Subset of W3C Document Object Model (DOM) APIs not covered by standard Java SE.")
<syntaxhighlight lang="text" class="" style="" inline="1">getCode("jdk.zipfs") </syntaxhighlight> getCode("ZIP file system provider allowing ZIP archives to be accessed as file systems.")


Integration with OSGi

The Java Module System does not intend to duplicate every aspect of the OSGi framework, such as its Life-Cycle management and Services Registry. Nevertheless, it offers capabilities not found in OSGi, including modularity enforced at compile-time and native library support natively integrated. In 2016, various articles exploring possible compatibility between the Java Module System and OSGi appeared on InfoQ and the OSGi Alliance Blog.

See also

External links