Sub topic 14
Automatic Modules
Assume you know Java packages, access modifiers, javac, java, JAR files, and the classpath. This chapter focuses on using an existing JAR on the module path before its source code has a module-info.java file.
An automatic module is a JAR placed on the module path without a module descriptor. Java creates a module descriptor from the JAR's name or its manifest. This makes gradual migration possible.
How Java chooses the module name
Java first looks for the Automatic-Module-Name entry in the JAR manifest. If it is absent, Java derives a name from the file name. For example, legacy-lib-2.4.jar might become legacy.lib, but the exact result depends on the file name.
A manifest gives the library a stable name:
Manifest-Version: 1.0
Automatic-Module-Name: com.example.legacyYou can create a JAR with that manifest after compiling its classes:
jar --create \
--file libs/legacy-lib.jar \
--manifest legacy-manifest.mf \
-C legacy-classes .Inspect the result:
jar --describe-module --file libs/legacy-lib.jarThe module name is part of your application's build contract. Prefer an explicit Automatic-Module-Name instead of depending on a generated name.
Requiring an automatic module
A named module can depend on the automatic module by using its inferred name:
module com.example.app {
requires com.example.legacy;
}Compile and run with both the application output and the legacy JAR on the module path:
javac -d out \
--module-path libs \
--module-source-path src \
-m com.example.app
java --module-path "out:libs" \
-m com.example.app/com.example.app.MainOn Windows, use ; instead of : as the path separator.
An automatic module has broad permissions:
- It reads every named module and the unnamed module.
- Every package in the JAR is exported.
- Every package is open for deep reflection.
- It can be required by a named module.
These rules make migration convenient, but they provide little encapsulation. A library's internal packages remain accessible until the library becomes a named module and adds explicit exports and opens directives.
| JAR form | Module name | Package access |
|---|---|---|
| JAR without `module-info.class` on the module path | Manifest name or derived name | All packages exported and open |
> callout note > An automatic module is a migration bridge, not a finished module design. Treat its name and package access as temporary until the library publishes a real module descriptor.
Common errors
module not found usually means the JAR is on the classpath instead of the module path, or the requires name is wrong. Run jar --describe-module to verify the name.
A JAR can also cause a split-package error when another module contains the same package. Move the package into one module, or redesign the package boundaries. Do not rely on automatic modules to hide this conflict.
Exercise
A library is named payments-1.0.jar and has no module-info.class or Automatic-Module-Name. What should you do before writing requires?
Show answer
Run jar --describe-module --file payments-1.0.jar and use the module name it reports. Prefer adding an explicit Automatic-Module-Name when you control the library.