The Java Platform Module System (JPMS) 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.
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 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, and again deferred to a Java 9 release in 2017. Java 9 including the Java Module System was released on September 21, 2017.
import module. For example,
import modulejava.base;, similarly, imports all 54 packages belonging to java.base.
colours = List.of(\"Red\", \"Orange\", \"Yellow\", \"Green\", \"Blue\", \"Indigo\", \"Violet\");\n IO.println(MessageFormat.format(\"My favourite colour is {0} and today is {1,date,long}\",\n colours.get(ThreadLocalRandom.current().nextInt(colours.size())),\n new Date()\n ));\n }\n}\n"}}'>packageorg.wikipedia.examples;
import modulejava.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
*/
publicclass Example{
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:
- open: indicates that all classes in a package are accessible via reflection by other modules
- requires: used in a module declaration to specify that the module depends on another module
- transitive: used with the with: used with the java.compiler
| Defines APIs related to the language model, Java annotation processing, and the Java compiler. |
| java.instrument |
Defines services that allow for the instrumentation of programs running on the Java Virtual Machine (JVM). |
| java.management.rmi |
Defines the Remote Method Invocation (RMI) connector for the Java Management Extensions (JMX) Remote API. |
| java.prefs |
Defines the API for managing and storing user preferences. |
| java.se |
Defines the standard API for the Java SE Platform. |
| java.sql.rowset |
Defines the JDBC RowSet API for managing and interacting with tabular data in Java. |
| java.xml.crypto |
Defines the API for cryptographic operations with XML data. |
| jdk.charsets |
Provides additional character sets (jdk.crypto.cryptoki |
Defines the implementation of the SunPKCS11 security provider for cryptographic operations. |
| jdk.hotspot.agent |
Provides the implementation for the HotSpot Serviceability Agent. |
| jdk.jartool |
Provides tools for manipulating Java Archive (JAR) files, such as the jar and jarsigner commands. |
| jdk.jconsole |
Defines the JMX-based graphical tool, jconsole, for monitoring and managing Java applications. |
| jdk.jdwp.agent |
Provides the implementation of the Java Debug Wire Protocol (JDWP) agent for remote debugging. |
| jdk.jpackage |
Defines the Java packager tool (jpackage) for creating platform-specific application packages. |
| jdk.jstatd |
Defines the jstatd tool for remotely monitoring JVM statistics. |
| jdk.management.agent |
Defines the JMX-based management agent for the JVM. |
| jdk.naming.rmi |
Implements the RMI Java Naming provider for remote method invocation. |
| jdk.sctp |
Provides the API for Stream Control Transmission Protocol (SCTP) in the JDK. |
| jdk.xml.dom |
Defines 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). |
| javafx.base |
Defines core APIs for the JavaFX UI toolkit (such as APIs for bindings, properties, collections, and events). |
| javafx.graphics |
Defines scenegraph, animation, geometry, and other related APIs for the JavaFX UI toolkit. |
| javafx.web |
Defines WebView APIs for the JavaFX UI toolkit. |
| jfx.incubator.richtext |
Incubates 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. A couple of articles exploring how the Java Module System and OSGi could interoperate were published in 2016. These can be found on InfoQ and also the OSGi Alliance Blog.