Java Platform Module System Explained

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.

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 a new way of grouping code. 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:

module com.foo.bar {
    requires com.foo.baz;
    exports com.foo.bar.alpha;
    exports com.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]

Links with OSGi

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.[15] A couple of articles exploring how the Java Module System and OSGi could interoperate were published in 2016. These can be found on InfoQ[16] and also the OSGi Alliance Blog.[17]

See also

External links

Notes and References

  1. Web site: Java Platform Module System (JSR 376). Oracle Corporation. 2018-07-02.
  2. Web site: Project Jigsaw . Oracle Corporation. 2015-11-29.
  3. Web site: It's time for … Plan B. Oracle Corporation. Mark Reinhold. 2009-09-20. 2017-06-21.
  4. Web site: JDK 9. Oracle Corporation. 2016-02-24.
  5. Web site: Java 9: Release date and new features. techworld.com. 2017-07-21. 2017-11-18.
  6. Web site: jlink: The Java Linker (JSR 282). Oracle Corporation. 2016-03-12.
  7. Web site: Java Platform Module System (JSR 376). Oracle Corporation. 2015-11-29.
  8. Web site: JEP 238: Multi-Release JAR Files. Oracle Corporation. 2017-07-31.
  9. Web site: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization. Oracle Corporation. 2017-07-31.
  10. Web site: JEP 260: Encapsulate Most Internal APIs. Oracle Corporation. 2017-07-31.
  11. Web site: JEP 275: Modular Java Application Packaging. Oracle Corporation. 2017-07-31.
  12. Web site: The State of the Module System. Oracle Corporation. Mark Reinhold. 2016-03-08. 2017-02-18.
  13. Web site: JEP 396: Strongly Encapsulate JDK Internals by Default . 2021-02-06.
  14. Web site: JDK Module Summary. Oracle Corporation. 2016-06-24. 2017-02-18. 2015-12-08. https://web.archive.org/web/20151208074800/http://cr.openjdk.java.net/~mr/jigsaw/ea/module-summary.html. dead.
  15. Web site: Project Jigsaw: Late for the train: The Q&A. Oracle Corporation. Mark Reinhold. 2012-08-24. 2015-11-29.
  16. Web site: Java 9, OSGi and the Future of Modularity. InfoQ. 2016-09-26.
  17. Web site: Java Module Layers and OSGi Bundles. OSGi Alliance. 2016-08-01.