Automatic Modules
A plain JAR—that is, a JAR that does not have a module-info.class file in its top-level directory—defines an automatic module when placed on the module path. An automatic module has a module name which is determined according to the scheme described below.
An automatic module can read all other modules, including the unnamed module. This means that if an explicit module reads a single automatic module, all other automatic modules can be implicitly read by the explicit module. This avoids having a requires directive to every automatic module required by a named module.
All packages in the automatic module are exported and open for reflection. Access to its types is solely determined by the accessibility rules for types in packages. Explicit modules can read automatic modules by specifying them in the requires directives, as they have a name.
An automatic module is a migration feature. Resorting to an automatic module should be a temporary measure. As such, a module should be migrated to an explicit module by defining an appropriate module descriptor.
Scheme for Naming Automatic Modules
If the JAR file has the attribute Automatic-Module-Name defined in its META-INF/ MANIFEST.MF file (p. 1212), its value is used as the module name. This is the recommended practice, as it avoids issues like name clashes.
If the JAR file does not have the attribute Automatic-Module-Name defined in its META-INF/MANIFEST.MF file, its name is derived from the file name of the JAR file according to the following algorithm:
- Remove the .jar suffix from the name.
- Remove any version information at the end of the name, which is usually specified in the version number format after a hyphen (-); for example, -1.2.3-ERC.
- All non-alphanumeric characters are replaced with a dot (.), any sequence of dots is replaced with a single dot, and all leading and trailing dots are removed. This step avoids constructing a module name with illegal characters.
As an example, the JAR file name ying-yang.jar of an automatic module will derive the module name ying.yang, as will the JAR file name ying-yang-1.2.3-ERC.jar. File names for plain JARs acting as automatic modules should be chosen with care so as to avoid name clashes when such JARs do not specify a unique module name in their manifest file.
Leave a Reply