Java Platform Module System

Last updated

The Java Platform Module System [1] specifies a distribution format for collections of Java code and associated resources. It also specifies a repository for storing these collections, or modules , and identifies how they can be discovered, loaded and checked for integrity. It includes features such as namespaces with the aim of fixing some of the shortcomings in the existing JAR format, especially the JAR Hell, which can lead to issues such as classpath and class loading problems.

Contents

The Java Module System was initially being developed under the Java Community Process as JSR 277 and was scheduled to be released with Java 7.

JSR 277 later was put on hold and Project Jigsaw [2] was created to modularize the JDK. This JSR was superseded by JSR 376 (Java Platform Module System).

Project Jigsaw was originally intended for Java 7 (2011) but was deferred to Java 8 (2014) as part of Plan B, [3] and again deferred to a Java 9 release in 2017. [4] Java 9 including the Java Module System was released on September 21, 2017. [5]

Architecture

The Java Module System implemented in Java 9 includes the following JEPs and JSR (Java Specification Request): [2]

Additionally, several other JDK 9 features have been added to ease transition to the module system:

Properties of modules

Modules are used to group packages and tightly control what packages belong to the public API. Contrary to Jar files, modules explicitly declare which modules they depend on, and what packages they export. [12] Explicit dependency declarations improve the integrity of the code, by making it easier to reason about large applications and the dependencies between software components.

The module declaration is placed in a file named module-info.java at the root of the module’s source-file hierarchy. The JDK will verify dependencies and interactions between modules both at compile-time and runtime.

For example, the following module declaration declares that the module com.foo.bar depends on another com.foo.baz module, and exports the following packages: com.foo.bar.alpha and com.foo.bar.beta:

modulecom.foo.bar{requirescom.foo.baz;exportscom.foo.bar.alpha;exportscom.foo.bar.beta;}

The public members of com.foo.bar.alpha and com.foo.bar.beta packages will be accessible by dependent modules. Private members are inaccessible even through a means such as reflection. Note that in Java versions 9 through 16, whether such 'illegal access' is de facto permitted depends on a command line setting. [13]

The JDK itself has been modularized in Java 9. [14] For example, the majority of the Java standard library is exported by the module java.base.

As of Java 25, modules can themselves be imported, automatically importing all exported packages. [15] This is done using import module. For example, importmodulejava.sql; is equivalent to

importjava.sql.*;importjavax.sql.*;// Remaining indirect exports from java.logging, java.transaction.xa, and java.xml

Similarly, importmodulejava.base;, similarly, imports all 54 packages belonging to java.base.

importmodulejava.base;/** * importing module java.base allows us to avoid manually importing most classes * the following classes (outside of java.lang) are used: * java.text.MessageFormat * java.util.Date * java.util.List * java.util.concurrent.ThreadLocalRandom */publicclassExample{publicstaticvoidmain(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())),newDate()));}}

Modules use the following keywords:

Standard modules

Core modules

The modules under namespace java.* belong to the Java Platform, Standard Edition, and modules under namespace jdk.* belong to the Java Development Kit. [16]

NameDescription
java.baseDefines the core APIs that form the foundation of the Java SE Platform.

Implicitly required by all modules and does not need to be declared with requires inside a module declaration.

java.compilerDefines APIs related to the language model, Java annotation processing, and the Java compiler.
java.datatransferDefines the APIs that facilitate data transfer between applications or within an application.
java.desktopDefines the Abstract Window Toolkit (AWT) and Swing libraries for user interfaces, along with APIs for accessibility, audio, imaging, printing, and JavaBeans.
java.instrumentDefines services that allow for the instrumentation of programs running on the Java Virtual Machine (JVM).
java.loggingDefines the API for logging events in Java applications (Java Logging API).
java.managementDefines the APIs for the Java Management Extensions (JMX) framework.
java.management.rmiDefines the Remote Method Invocation (RMI) connector for the Java Management Extensions (JMX) Remote API.
java.namingDefines the API for Java Naming and Directory Interface (JNDI) services.
java.net.httpDefines the API for HTTP client and WebSocket functionality.
java.prefsDefines the API for managing and storing user preferences.
java.rmiDefines the Remote Method Invocation (RMI) API for object communication across Java virtual machines.
java.scriptingDefines the API for integrating scripting engines into Java applications (Scripting API).
java.seDefines the standard API for the Java SE Platform.
java.security.jgssDefines the Java binding for the IETF Generic Security Services API (GSS-API) for security contexts.
java.security.saslDefines Java's support for the Simple Authentication and Security Layer (SASL) protocol.
java.smartcardioDefines the API for interacting with smart card devices (Java Smart Card I/O API).
java.sqlDefines the Java Database Connectivity (JDBC) API for database interaction.
java.sql.rowsetDefines the JDBC RowSet API for managing and interacting with tabular data in Java.
java.transaction.xaDefines an API for managing distributed transactions within JDBC.
java.xmlDefines the Java API for XML Processing (JAXP), including tools for parsing and transforming XML data.
java.xml.cryptoDefines the API for cryptographic operations with XML data.
jdk.accessibilityDefines utility classes for implementing Assistive Technologies in the JDK.
jdk.attachDefines the attach API for connecting to and interacting with a running JVM instance (attach API).
jdk.charsetsProvides additional character sets (charsets), including double-byte and IBM-specific charsets, not included in java.base.
jdk.compilerDefines the internal implementation of the Java compiler and the command-line tool javac .
jdk.crypto.cryptokiDefines the implementation of the SunPKCS11 security provider for cryptographic operations.
jdk.dynalinkDefines the API for dynamically linking high-level operations on objects.
jdk.editpadImplements the edit pad service used by jdk.jshell for evaluating code.
jdk.hotspot.agentProvides the implementation for the HotSpot Serviceability Agent.
jdk.httpserverDefines the JDK-specific HTTP server API and the jwebserver tool for running a simple HTTP server.
jdk.incubator.vectorDefines the API for expressing computations that can be compiled into SIMD instructions at runtime, such as AVX or NEON instructions.
jdk.jartoolProvides tools for manipulating Java Archive (JAR) files, such as the jar and jarsigner commands.
jdk.javadocDefines the system documentation tool implementation and its command-line version, javadoc .
jdk.jcmdDefines tools for diagnosing and troubleshooting JVM processes, including jcmd, jps, and jstat.
jdk.jconsoleDefines the JMX-based graphical tool, jconsole , for monitoring and managing Java applications.
jdk.jdepsDefines tools for analysing dependencies in Java programs and libraries, such as jdeps, javap, jdeprscan, and jnativescan.
jdk.jdiDefines the Java Debug Interface (JDI) for debugging Java applications.
jdk.jdwp.agentProvides the implementation of the Java Debug Wire Protocol (JDWP) agent for remote debugging.
jdk.jfrDefines the API for JDK Flight Recorder (JFR) for collecting performance and diagnostic data.
jdk.jlinkDefines the jlink tool for creating custom runtime images, and jmod and jimage tools for managing JMOD and JDK container files.
jdk.jpackageDefines the Java packager tool (jpackage) for creating platform-specific application packages.
jdk.jshellProvides the jshell tool for interactively evaluating Java code snippets.
jdk.jsobjectDefines the API for integrating JavaScript objects in Java applications.
jdk.jstatdDefines the jstatd tool for remotely monitoring JVM statistics.
jdk.localedataProvides locale-specific data for non-US locales.
jdk.managementDefines JDK-specific management interfaces for JVM monitoring and control.
jdk.management.agentDefines the JMX-based management agent for the JVM.
jdk.management.jfrDefines the management interface for JDK Flight Recorder (JFR).
jdk.naming.dnsImplements the DNS Java Naming provider for resolving domain names.
jdk.naming.rmiImplements the RMI Java Naming provider for remote method invocation.
jdk.netDefines the JDK-specific API for network programming.
jdk.nio.mapmodeDefines specific file mapping modes available in the JDK.
jdk.sctpProvides the API for Stream Control Transmission Protocol (SCTP) in the JDK.
jdk.security.authImplements authentication modules and security-related interfaces within the javax.security.auth package (interfaces in javax.security.auth.*).
jdk.security.jgssDefines extensions to the GSS-API and the implementation of the SASL GSSAPI mechanism in the JDK.
jdk.xml.domDefines the JDK's subset of the World Wide Web Consortium (W3C) Document Object Model (DOM) API not covered by Java SE. Exports packages outside of the java namespace (from org.w3c.dom).
jdk.zipfsProvides the implementation for the ZIP file system provider, enabling access to ZIP files as file systems.

Jakarta EE, formerly part of the Java standard library, is not modularised, however build systems can generate automatic modules for it.

JavaFX modules

JavaFX was previously bundled with the core JDK, until Java 11 when it was split into OpenJFX. JavaFX is split into the following modules. [17]

NameDescription
javafx.baseDefines core APIs for the JavaFX UI toolkit (such as APIs for bindings, properties, collections, and events).
javafx.controlsDefines the APIs for UI controls, charts, skins in the JavaFX UI toolkit.
javafx.fxmlDefines the FXML APIs in the JavaFX UI toolkit.
javafx.graphicsDefines scenegraph, animation, geometry, and other related APIs for the JavaFX UI toolkit.
javafx.mediaDefines media playback and audio content APIs for the JavaFX UI toolkit.
javafx.swingDefines JavaFX/Swing interop support APIs for the JavaFX UI toolkit.
javafx.webDefines WebView APIs for the JavaFX UI toolkit.
jdk.jsobjectDefines APIs for JavaScript objects.
jfx.incubator.inputIncubates mechanism for customising JavaFX controls.
jfx.incubator.richtextIncubates RichTextArea control for JavaFX.

The Java Module System does not intend to support all the functionalities that the OSGi platform currently supports (for example the Life-Cycle model and the Services Registry). However the Java Module System will support functions which are not supported by OSGi, such as modularity at compile-time, and built-in support for native libraries. [18] A couple of articles exploring how the Java Module System and OSGi could interoperate were published in 2016. These can be found on InfoQ [19] and also the OSGi Alliance Blog. [20]

See also

References

  1. "Java Platform Module System (JSR 376)". Oracle Corporation . Retrieved 2018-07-02.
  2. 1 2 "Project Jigsaw". Oracle Corporation . Retrieved 2015-11-29.
  3. Mark Reinhold (2009-09-20). "It's time for … Plan B". Oracle Corporation . Retrieved 2017-06-21.
  4. "JDK 9". Oracle Corporation . Retrieved 2016-02-24.
  5. "Java 9: Release date and new features". techworld.com. 2017-07-21. Retrieved 2017-11-18.
  6. "jlink: The Java Linker (JSR 282)". Oracle Corporation . Retrieved 2016-03-12.
  7. "Java Platform Module System (JSR 376)". Oracle Corporation . Retrieved 2015-11-29.
  8. "JEP 238: Multi-Release JAR Files". Oracle Corporation . Retrieved 2017-07-31.
  9. "JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization". Oracle Corporation . Retrieved 2017-07-31.
  10. "JEP 260: Encapsulate Most Internal APIs". Oracle Corporation . Retrieved 2017-07-31.
  11. "JEP 275: Modular Java Application Packaging". Oracle Corporation . Retrieved 2017-07-31.
  12. Mark Reinhold (2016-03-08). "The State of the Module System". Oracle Corporation . Retrieved 2017-02-18.
  13. "JEP 396: Strongly Encapsulate JDK Internals by Default" . Retrieved 2021-02-06.
  14. "JDK Module Summary". Oracle Corporation. 2016-06-24. Archived from the original on 2015-12-08. Retrieved 2017-02-18.
  15. "JEP 494: Module Import Declarations (Second Preview)". openjdk.org.
  16. "Java® Platform, Standard Edition & Java Development Kit Version 24 API Specification". docs.oracle.com.
  17. "Overview (JavaFX)". openjfx.io.
  18. Mark Reinhold (2012-08-24). "Project Jigsaw: Late for the train: The Q&A". Oracle Corporation . Retrieved 2015-11-29.
  19. "Java 9, OSGi and the Future of Modularity". InfoQ. Retrieved 2016-09-26.
  20. "Java Module Layers and OSGi Bundles". OSGi Alliance. Retrieved 2016-08-01.