Sub topic 14
Automatic modules
An automatic module is a plain jar file placed on the module path. The jar has no module-info.class, but the runtime still treats it as a named module. Automatic modules let a modular application depend on libraries that have not been modularized yet.
This chapter assumes you know requires, exports, the module path, and the difference between the classpath and the module path. It also assumes you have read the chapter on the unnamed module.
What makes a jar automatic
A jar becomes an automatic module when two conditions hold:
- It sits on the module path (
--module-path, or-p), not on the classpath. - It has no module descriptor.
The runtime then derives a module from the jar and gives it three properties:
- A name. The name comes from the jar's manifest or, failing that, from the file name.
- Exports. Every package in the jar is exported, and every package is open for deep reflection.
- Readability. The module reads every other module, including the unnamed module, which holds classpath code.
An automatic module therefore has no encapsulation. Anything public in it is reachable. That is the price of letting an unmodularized jar take part in the module graph.
How the module name is chosen
The name is picked in this order:
- If the manifest has an
Automatic-Module-Nameheader, its value is the name. - Otherwise the name is derived from the file name.
For the file-name rule, the runtime removes the .jar extension and removes a trailing version, which is a hyphen followed by a digit and everything after it. It then replaces each run of non-alphanumeric characters with a dot.
| File name | Derived module name |
|---|---|
| `legacy-lib.jar` | `legacy.lib` |
| `commons-lang3-3.12.0.jar` | `commons.lang3` |
| `guava-31.1-jre.jar` | `guava` |
| `my_tool.jar` | `my.tool` |
File-name names are fragile. Renaming the jar changes the module name, and any requires clause that used the old name stops working. Names derived this way can also contain segments that are not valid Java identifiers, so you may not be able to write a requires for them.
A library author fixes this by adding one line to the manifest:
Automatic-Module-Name: com.example.legacyThis line commits the library to a stable name. It does not require any code change. Later, when the library adds a real module-info.java, it should use the same name so that clients do not break.
Using an automatic module
Suppose legacy-lib.jar contains a com.legacy.util.Strings class. The application module requires it by its derived name:
// src/com.example.app/module-info.java
module com.example.app {
requires legacy.lib;
}// src/com.example.app/com/example/app/Main.java
package com.example.app;
import com.legacy.util.Strings;
public class Main {
public static void main(String[] args) {
System.out.println(Strings.shout("modules"));
}
}Compile and run with the jar on the module path next to the compiled modules:
javac -d out --module-path libs/legacy-lib.jar --module-source-path src -m com.example.app
java --module-path out:libs/legacy-lib.jar -m com.example.app/com.example.app.MainOn Windows, separate path entries with ; instead of :.
Because the automatic module exports every package, Strings is accessible without any further declaration.
Readability rules
An automatic module reads all other named modules and the unnamed module. This has two consequences.
First, if the legacy jar depends on other jars, those jars must also be resolvable. When any automatic module is resolved, the runtime adds all automatic modules on the module path to the module graph. This avoids missing-dependency failures, but it can pull in more jars than you expect.
Second, your named modules do not automatically read an automatic module. You still need a requires clause for it. The exception is the unnamed module, which reads everything.
The diagram below shows the relationships.
Transitive dependencies
An automatic module cannot say requires transitive, because it has no descriptor. Instead, readability is implied. If com.example.app requires legacy.lib, it also reads every other automatic module that legacy.lib might use. This is a special rule for automatic modules. It lets your code use types from a library's dependencies without listing each dependency.
A named module that requires an automatic module should still list the other automatic modules it uses directly. Relying on the implied rule hides dependencies and breaks once those libraries become real modules.
Common errors
module not found: legacy.lib. The jar is missing from the module path, or the derived name differs from what you wrote. Run this to see the name the runtime derives:
jar --describe-module --file libs/legacy-lib.jarThe output includes a line such as legacy.lib automatic.
The package is accessible, but it moved to the classpath. If you put the same jar on the classpath, it joins the unnamed module, and a named module cannot requires it. Move it to the module path.
Split packages. If an automatic module and another module contain the same package, resolution fails. The split packages chapter covers fixes.
Warnings about names. The compiler can warn when a module requires an automatic module whose name comes from the file name. Treat this as a sign to ask the library for an Automatic-Module-Name or to pin the file name.
Library authors: a note on the stable name
If you publish a library, a missing automatic name is a hidden public contract. Clients will pick a name from your file name, and you cannot change the file name later without breaking them. Adding Automatic-Module-Name early is a small step toward modularization.
With Maven, you can set it in the jar plugin configuration:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>com.example.legacy</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
</plugin>Limits of automatic modules
- They cannot hide packages, so nothing is encapsulated.
- Their name may be unstable.
- They cannot be used by
jlink, becausejlinkrequires explicit modules. Linking an application that depends on automatic modules fails. - They cannot declare
usesorprovides. Services in a legacy jar'sMETA-INF/servicesfiles are still found byServiceLoaderwhen the jar is an automatic module.
Because of the jlink limit, automatic modules suit a transition period. For a final modular application, replace them with real modules, or generate a descriptor for the jar with jdeps --generate-module-info.
Exercise
A jar named report-tools-2.4.1.jar is on the module path and has no Automatic-Module-Name header. What module name would a requires clause need? Name two reasons a library author should add the header.
Show answer
The derived name is report.tools, because the version suffix -2.4.1 is removed and the hyphen becomes a dot. The author should add the header because (1) the name then no longer depends on the file name, so renaming or re-versioning the jar cannot break clients, and (2) it reserves the name that the library will use when it later gets a real module-info.java.
Exercise
Your application module requires an automatic module. You then run jlink on it and the command fails. Why, and what are two ways forward?
Show answer
jlink only links explicit modules, and an automatic module has no descriptor. You can either replace the jar with a version that includes a module-info.class, or generate a descriptor with jdeps --generate-module-info and add it to the jar.