Migrating to Java Modules: Unnamed and Automatic Modules
Migrating to modules means moving an existing application from the class path to the module path piece by piece, without rewriting every library at once. JPMS offers two "transitional" kinds of modules for this: the unnamed module, which receives all class-path code, and the automatic module, which is what an ordinary JAR without module-info.class becomes when placed on the module path.
jar --describe-module --file mp/json-utils-2.3.1.jar
No module descriptor found. Derived automatic module.
json.utils@2.3.1 automatic
requires java.base mandated
contains com.legacy.json This lesson closes the module: it builds on module-info, requires, exports and opens and on the jlink lesson. Below: the properties of the three kinds of modules, the naming rules for automatic modules, two migration strategies, the --add-reads and --add-exports options, and typical errors. All examples were checked on JDK 25.
1. Three kinds of modules: named, automatic, unnamed
| Named (explicit) | Automatic | Unnamed | |
|---|---|---|---|
| What it is | A JAR or directory with module-info.class on the module path | A JAR without module-info.class on the module path | Everything on the class path |
| Name | From module-info.java | From Automatic-Module-Name or the file name | None (getName() returns null) |
| Modules it reads | Only those in requires (plus java.base) | All modules, including the unnamed one | All modules |
| What it exports | Only the packages in exports | All packages | All packages |
| What is open to reflection | Only the packages in opens | All packages | All packages |
| Who can read it | Any module through requires | Any module through requires by its name | Only automatic modules and the class path itself; a named module cannot |
The main migration rule follows from the last row: a named module cannot depend on class-path code. An automatic module is the bridge between them: it has a name, so it can appear in requires, and in turn it sees the class path.
2. The unnamed module: class-path code
Every class loaded from the class path belongs to the unnamed module. That is why old applications run unchanged on Java 9+: the unnamed module reads all JDK modules and exports all its packages, so JPMS checks effectively do not apply to it. Properties worth remembering:
- It has no name.
getClass().getModule().getName()returnsnull, and error messages call itunnamed module @0x776ec8df. - A modular JAR on the class path loses its modularity. The
module-info.classinside it is ignored, and its classes also end up in the unnamed module, just as withjava -jar. - A named module does not read it. You cannot write anything like "requires classpath" in
module-info.java: the unnamed module has no name to refer to.
If the module com.shop.export uses a class from a JAR on the class path, it fails both at compile time and at run time:
error: package com.legacy.text is not visible
(package com.legacy.text is declared in the unnamed module, but module com.shop.export does not read it)
Exception in thread "main" java.lang.IllegalAccessError: class com.shop.export.Exporter (in module com.shop.export)
cannot access class com.legacy.text.Slug (in unnamed module @0x776ec8df) because module com.shop.export does not read unnamed module @0x776ec8df The only workaround is the --add-reads com.shop.export=ALL-UNNAMED option at compile time and run time (section 8). The proper fix is to move the library to the module path, where it becomes an automatic module.
3. Automatic modules: a plain JAR on the module path
Put an ordinary JAR on the module path, and the JVM makes up a module descriptor for it. An automatic module:
- exports and opens all its packages, so code that requires it sees all public classes, and reflection with
setAccessible(true)works even onprivatefields; - reads all modules, including the unnamed one, so it can use libraries that stayed on the class path;
- grants implied readability to all other automatic modules: a module that requires one automatic module automatically reads all the others (similar to
requires transitivefor every automatic module at once); - turns
META-INF/servicesfiles intoprovidesdirectives (see Java Services).
There is a limit too: an automatic module enters the graph only if something requires it with requires or it is listed in --add-modules. If json-utils-2.3.1.jar is on the module path but the main module does not require it, ModuleLayer.boot().findModule("json.utils").isPresent() returns false; with --add-modules json.utils it returns true. On the other hand, as soon as one automatic module is resolved, the JVM adds all automatic modules on the module path to the graph: with --add-modules json.utils, text.tools shows up too, although nothing mentions it.
4. How an automatic module's name is derived
The name comes from two sources, in order:
- The
Automatic-Module-Nameattribute inMETA-INF/MANIFEST.MF, if present. Library authors use it to "reserve" a stable module name without moving tomodule-info: withAutomatic-Module-Name: com.legacy.text, the filetext-tools-1.0.jarbecomes the modulecom.legacy.text. - The file name, if there is no attribute. The
.jaris dropped; a part starting with a hyphen followed by a digit (-1.0,-2.3.1) is treated as the version and cut off; every character other than letters and digits is replaced with a dot; repeated dots collapse into one, and leading and trailing dots are removed.
Results for real file names (output of jar --describe-module, JDK 25):
| File | Module name | Version |
|---|---|---|
commons-lang3-3.12.0.jar | commons.lang3 | 3.12.0 |
log4j-api-2.20.0.jar | log4j.api | 2.20.0 |
spring-core-6.1.0.jar | spring.core | 6.1.0 |
guava-33.0.0-jre.jar | guava | 33.0.0-jre |
my.util-2.0-SNAPSHOT.jar | my.util | 2.0-SNAPSHOT |
foo_bar.jar | foo.bar | — |
legacy_utils-v2.jar | legacy.utils.v2 (-v2 is not a version: a letter follows the hyphen) | — |
a..b-1.jar | a.b | 1 |
json-int-utils.jar | Error: 'int' is not a Java identifier | — |
1lib.jar | Error: '1lib' is not a Java identifier | — |
A name derived from the file name is fragile: rename the JAR and the module name changes, breaking every requires. That is why adding Automatic-Module-Name to the manifest is the first step every library author should take.
5. Example: the shop and two legacy libraries
The shop has two non-modular libraries. text-tools-1.0.jar contains the class com.legacy.text.Slug, and json-utils-2.3.1.jar contains com.legacy.json.Json, which calls Slug and has a private field secret. The new export module has already been moved to modules:
// src/com.shop.export/module-info.java
module com.shop.export {
requires json.utils; // automatic module from json-utils-2.3.1.jar
}
// src/com.shop.export/com/shop/export/Exporter.java
package com.shop.export;
import com.legacy.json.Json;
import java.lang.reflect.Field;
public class Exporter {
public static void main(String[] args) throws Exception {
System.out.println(Json.field("Product Name", "book"));
System.out.println(Json.class.getModule().getName());
System.out.println(com.legacy.text.Slug.class.getModule().getName());
Field f = Json.class.getDeclaredField("secret");
f.setAccessible(true);
System.out.println(f.get(new Json()));
}
} Option 1: both libraries on the module path.
javac -p mp -d out --module-source-path src -m com.shop.export
java -p "mp;out" -m com.shop.export/com.shop.export.Exporter
"product-name": "book"
json.utils
text.tools
s3 This output shows three properties of automatic modules. com.shop.export requires only json.utils but also uses Slug from text.tools: that is the implied readability of automatic modules. The text.tools module entered the graph although nobody requires it explicitly: all automatic modules were resolved together with json.utils. And the private field was read without any opens, because an automatic module is fully open.
Option 2: json-utils on the module path, text-tools on the class path.
java -p "mp2;out" -cp cpl/text-tools-1.0.jar -m com.shop.export/com.shop.export.Exporter
"product-name": "book"
json.utils
Exception in thread "main" java.lang.IllegalAccessError: class com.shop.export.Exporter (in module com.shop.export)
cannot access class com.legacy.text.Slug (in unnamed module @0x776ec8df) ... The first line is printed: Json.field() calls Slug from the class path internally, and that is allowed because an automatic module reads the unnamed module. But the direct use of Slug by com.shop.export fails: a named module does not read the unnamed one. The compiler would catch it even earlier: package com.legacy.text is declared in the unnamed module, but module com.shop.export does not read it.
6. Automatic module errors
| Situation | Message |
|---|---|
| Two versions of one library on the module path | FindException: Two versions of module text.tools found in dup (text-tools-2.0.jar and text-tools-1.0.jar) |
| The JAR contains a class without a package | FindException: Unable to derive module descriptor for mp4\top-util.jar, caused by unnamed package not allowed in module |
| No module name can be derived from the file name | Invalid module name: 'int' is not a Java identifier |
| A named and an automatic module contain the same package | module com.shop.app2 reads package com.legacy.text from both com.shop.text and text.tools |
An automatic module is passed to jlink | Error: automatic module cannot be used with jlink |
Split packages are the most common migration problem. On the class path, identical packages from different JAR files were simply merged, but on the module path a package can belong to only one module. Such libraries have to be merged or their packages renamed before moving to modules.
7. Migration strategies: bottom-up and top-down
| Bottom-up | Top-down | |
|---|---|---|
| Where you start | With the lowest-level libraries, which do not depend on your other code | With the application itself |
| What becomes a named module | The lowest libraries, then layer by layer upward | The application's modules |
| Where code not yet migrated lives | On the class path, in the unnamed module | On the module path, as automatic modules |
| When it fits | All libraries are yours and can be changed | You use third-party libraries you do not control |
Why: when moving bottom-up, each new named module depends only on modules already migrated, while the code above still lives on the class path and sees everything. When moving top-down, your modules require third-party libraries from day one, so those libraries need names, and automatic modules provide them.
jdeps helps with both strategies: it shows dependencies and generates a draft module-info.java:
jdeps --module-path mp --generate-module-info gen mp/json-utils-2.3.1.jar
writing to gen\json.utils\module-info.java
module json.utils {
requires text.tools;
exports com.legacy.json;
} jdeps found the dependency on text.tools and exported the only package. Review the draft by hand: not everything should be exported, some requires may need to be requires transitive, and frameworks that use reflection need opens.
8. The --add-reads, --add-exports and --add-modules options
While migration is in progress, the JPMS rules can be relaxed temporarily with javac and java options, without changing module-info.java:
| Option | What it does | Example |
|---|---|---|
--add-reads M=N | Module M starts reading module N | --add-reads com.shop.export=ALL-UNNAMED |
--add-exports M/P=N | Module M exports package P to module N | --add-exports com.shop.catalog/com.shop.catalog.internal=ALL-UNNAMED |
--add-opens M/P=N | The same for deep reflection | --add-opens java.base/java.lang=ALL-UNNAMED |
--add-modules M | Adds a module to the graph when nothing requires it | --add-modules com.shop.catalog |
ALL-UNNAMED means "all class-path code". Example: an old class Old on the class path uses an internal package of the com.shop.catalog module:
javac -p aeo --add-modules com.shop.catalog --add-exports com.shop.catalog/com.shop.catalog.internal=ALL-UNNAMED -d lac Old.java
java -p aeo --add-modules com.shop.catalog --add-exports com.shop.catalog/com.shop.catalog.internal=ALL-UNNAMED -cp lac Old
15 The options must be repeated both at compile time and at run time: they are not recorded in the .class file. Without --add-exports at run time the same code fails with IllegalAccessError: ... because module com.shop.catalog does not export com.shop.catalog.internal to unnamed module, and without --add-modules with NoClassDefFoundError: a module nobody requires is not loaded at all when the main class is on the class path.
These options are a temporary measure. Each one means a module boundary is broken, and the goal of migration is to get rid of them by fixing module-info.java or the code.
Frequently asked questions
What is the difference between an automatic and an unnamed module?
Both export all packages and read all modules, but an automatic module has a name, from Automatic-Module-Name or the file name. So a named module can write requires json.utils and use an automatic module, while the unnamed module cannot be referred to. A JAR on the module path becomes an automatic module; everything on the class path forms the unnamed module.
Can a named module use a JAR from the class path?
Not directly: a named module does not read the unnamed module, and the compiler reports "declared in the unnamed module, but module ... does not read it". Put the JAR on the module path so it becomes an automatic module, and add requires. A temporary workaround is the --add-reads module=ALL-UNNAMED option at compile time and run time.
Why should a library have an Automatic-Module-Name attribute?
It sets a stable module name while the library has no module-info.java. Without it, the name is derived from the file name and changes when the JAR is renamed, and some names (for example, with an int part) yield no module at all. With the attribute, users can write requires without worrying that the name will change when the library becomes a full module.
Where should I start migrating a large project?
If third-party libraries are not modular, go top-down: migrate the application's modules and put the libraries on the module path as automatic modules. If all libraries are yours, bottom-up is easier. In both cases run jdeps first to find dependencies, split packages and uses of JDK internal APIs (--jdk-internals).
Why is an automatic module not loaded even though it is on the module path?
Only what is needed enters the module graph: an automatic module is loaded if some module requires it with requires or it is listed in --add-modules. Once at least one automatic module is loaded, the JVM adds all the other automatic modules on the module path as well. If none is requested, ModuleLayer.boot().findModule(...) returns an empty result.
Comments