Analysis of java9 module related options

  java9

Order

This article mainly analyzes java9 module related options

Module classification

unnamed module

All jars in classpath (whether modular or not) together form an unnamed module

Unnamed modules declare dependency on all named modules and exports all their own packages, but a named module cannot declare dependency on unnamed modules. If a package is defined in both named and unnamed modules, the package in named is used.

By default –illegal-access=permit = permit, which allows unnamed modules reflection (java.lang.reflect/java.lang.invoke) to use classes in all named modules
This option is mainly designed to facilitate migration to java9, but may be removed in java10

named module

The module with module-info.java is the regular module of java9
There is no jar package for module-info.java. If placed under module-path, java9 module system will automatically change it into automatic module (If the MANIFEST.MF file in the jar package has the Automatic-Module-Name attribute, take its value as the module name, if not, extract the module name from the jar package file name according to certain rules, and if the extraction is unsuccessful, it cannot be converted into an automatic module.)

automatic module

  • The default export /open all their own package
  • Transitive relies on jdk, other automatic module and its own modules and all other existing modules.
  • You can access classpath classes that are unnamed module.
  • Jlink does not support linking automatic modules

main module

The module containing the main method, specified through the –module

root module

The root module of the module system analysis, from the root module analysis (At compile time, not at run time) Module dependency, modules other than JDK default root modules can be added to module dependency analysis through-Add –add-modules mod1,mod2, and relevant dependent modules can be analyzed at the same time by scanning module descriptors.

The default root module for JDK9 is the java.se module.

java -d java.se
java.se@9
requires java.sql transitive
requires java.scripting transitive
requires java.logging transitive
requires java.security.jgss transitive
requires java.management transitive
requires java.instrument transitive
requires java.naming transitive
requires java.xml transitive
requires java.compiler transitive
requires java.desktop transitive
requires java.sql.rowset transitive
requires java.management.rmi transitive
requires java.base mandated
requires java.rmi transitive
requires java.xml.crypto transitive
requires java.datatransfer transitive
requires java.prefs transitive
requires java.security.sasl transitive

Since java.se.ee is not in the default root modules, the most common use of-add-modules is to add modules in ee, such as javv.xml.bind

java -d java.se.ee
java.se.ee@9
requires java.xml.bind transitive
requires java.base mandated
requires java.corba transitive
requires java.activation transitive
requires java.transaction transitive
requires java.xml.ws transitive
requires java.se transitive
requires java.xml.ws.annotation transitive

layers

Layer is to module what classloader is to class, and layer is a loading and instantiation mechanism of module. Usually used to load modules dynamically at runtime
Boot layer is the first layer used by java module system. It includes bootstrap loader, plateloader, application loader. The java runtime builds a module dependency graph based on the root modules specified by –add-modules, which is the boot layer.

Related options

–module

–module specifies the module that contains the main method

–add-modules

-add-modules add modules to the root set to facilitate parsing module dependencies

- ALL-DEFAULT,添加unnamed modules的默认root module添加到root set
- ALL-SYSTEM,添加系统模块(java,jdk,javafx等开头的模块,即jdk自身带的模块)到root set
- ALL-MODULE,添加module-path上所有可读的module到root set

–module-path

Module-path can be divided into three categories.

  • Application module path, specified through –module-path
  • Compilation module path, specified through –module-source-path-path, used with javac.
  • Upgrade module path, specified through-upgrade-module-path

–module-path changes jar without module declaration to automatic module;; Module-path can be a class/jar directory, jar, jmod directory

–path-module

It is used to add/overwrite the class file in the directory or jar package to the specified module. it is usually used in the test phase.

java --path-module targetModuleName=jarDir/replace.jar --module-path moduleDir -m com.example.module/com.example.module.Main

Add or overwrite class in replace.jar to targetModule

–limit-modules

Used to narrow the scope of the module, such as

java --limit-modules java.se.ee --list-modules
java.activation@9
java.base@9
java.compiler@9
java.corba@9
java.datatransfer@9
java.desktop@9
java.instrument@9
java.logging@9
java.management@9
java.management.rmi@9
java.naming@9
java.prefs@9
java.rmi@9
java.scripting@9
java.se@9
java.se.ee@9
java.security.jgss@9
java.security.sasl@9
java.sql@9
java.sql.rowset@9
java.transaction@9
java.xml@9
java.xml.bind@9
java.xml.crypto@9
java.xml.ws@9
java.xml.ws.annotation@9
jdk.httpserver@9
jdk.unsupported@9

It is used to limit the modules that can be used during compilation and runtime. The limited scope is the modules added by main module, –add-modules, and the modules specified by this parameter and the modules that are dependent on its transitive.
When the module containing the main method is unnamed modules, this parameter is often used to reduce the number of modules that need to be parsed.

–add-exports(Compile time+runtime)

When used with javac, it is allowed to be accessed at compile time.

javac -d outputDir --add-exports java.base/sun.net=com.example --module-source-path src $(find . -name "*.java")

When used with java, it allows runtime access

java --module-path outputDir --add-exports java.base/sun.net=com.example -m com.example/com.example.Main

jmod

Used to create and view jmod files

cd $JAVA_HOME
➜  Home ./bin/jmod list ./jmods/java.logging.jmod
classes/module-info.class
classes/java/util/logging/ConsoleHandler.class
classes/java/util/logging/ErrorManager.class
classes/java/util/logging/FileHandler$1.class
classes/java/util/logging/FileHandler$InitializationErrorManager.class
classes/java/util/logging/FileHandler$MeteredStream.class
classes/java/util/logging/FileHandler.class
classes/java/util/logging/Filter.class
classes/java/util/logging/Formatter.class
classes/java/util/logging/Handler$1.class
classes/java/util/logging/Handler.class
classes/java/util/logging/Level$1.class
classes/java/util/logging/Level$KnownLevel.class
classes/java/util/logging/Level$RbAccess.class
classes/java/util/logging/Level.class
classes/java/util/logging/Logger$1.class
classes/java/util/logging/Logger$ConfigurationData.class
classes/java/util/logging/Logger$LoggerBundle.class
classes/java/util/logging/Logger$RbAccess.class
classes/java/util/logging/Logger$SystemLoggerHelper$1.class
classes/java/util/logging/Logger$SystemLoggerHelper.class
classes/java/util/logging/Logger.class
classes/java/util/logging/Logging.class
classes/java/util/logging/LoggingMXBean.class
classes/java/util/logging/LoggingPermission.class
classes/java/util/logging/LogManager$1.class
classes/java/util/logging/LogManager$2.class
classes/java/util/logging/LogManager$3.class
classes/java/util/logging/LogManager$4.class
classes/java/util/logging/LogManager$5.class
classes/java/util/logging/LogManager$6.class
classes/java/util/logging/LogManager$7.class
classes/java/util/logging/LogManager$Cleaner.class
classes/java/util/logging/LogManager$CloseOnReset.class
classes/java/util/logging/LogManager$ConfigProperty.class
classes/java/util/logging/LogManager$LoggerContext$1.class
classes/java/util/logging/LogManager$LoggerContext.class
classes/java/util/logging/LogManager$LoggerWeakRef.class
classes/java/util/logging/LogManager$LoggingProviderAccess.class
classes/java/util/logging/LogManager$LogNode.class
classes/java/util/logging/LogManager$ModType.class
classes/java/util/logging/LogManager$RootLogger.class
classes/java/util/logging/LogManager$SystemLoggerContext.class
classes/java/util/logging/LogManager$VisitedLoggers.class
classes/java/util/logging/LogManager.class
classes/java/util/logging/LogRecord$CallerFinder.class
classes/java/util/logging/LogRecord.class
classes/java/util/logging/MemoryHandler.class
classes/java/util/logging/SimpleFormatter.class
classes/java/util/logging/SocketHandler.class
classes/java/util/logging/StreamHandler$1.class
classes/java/util/logging/StreamHandler.class
classes/java/util/logging/XMLFormatter.class
classes/sun/net/www/protocol/http/logging/HttpLogFormatter.class
classes/sun/util/logging/internal/LoggingProviderImpl$JULWrapper.class
classes/sun/util/logging/internal/LoggingProviderImpl$LogManagerAccess.class
classes/sun/util/logging/internal/LoggingProviderImpl.class
classes/sun/util/logging/resources/logging.class
classes/sun/util/logging/resources/logging_de.class
classes/sun/util/logging/resources/logging_es.class
classes/sun/util/logging/resources/logging_fr.class
classes/sun/util/logging/resources/logging_it.class
classes/sun/util/logging/resources/logging_ja.class
classes/sun/util/logging/resources/logging_ko.class
classes/sun/util/logging/resources/logging_pt_BR.class
classes/sun/util/logging/resources/logging_sv.class
classes/sun/util/logging/resources/logging_zh_CN.class
classes/sun/util/logging/resources/logging_zh_HK.class
classes/sun/util/logging/resources/logging_zh_TW.class
conf/logging.properties
legal/COPYRIGHT
legal/LICENSE
➜  Home ./bin/jmod describe ./jmods/java.logging.jmod
java.logging@9
exports java.util.logging
requires java.base mandated
provides jdk.internal.logger.DefaultLoggerFinder with sun.util.logging.internal.loggingproviderimpl
contains sun.net.www.protocol.http.logging
contains sun.util.logging.internal
contains sun.util.logging.resources
platform macos-amd64

Summary

Understanding the classification of modules and their related options can help us to better use the module system of java9.

doc