Sub topic 13
The unnamed module and classpath code
This chapter assumes you know how module-info.java works, how requires and exports shape the module graph, and what the classpath is. It covers what happens to code that is not in a named module, and how that code interacts with modules.
Where classpath code goes
Every class the JVM loads belongs to a module. Classes loaded from the classpath have no descriptor, so the runtime places them in the unnamed module. There is one unnamed module for each class loader. In a typical application, the application class loader has a single unnamed module that holds all classpath jars and directories.
This is what lets pre-Java 9 applications keep running. The unnamed module is deliberately permissive:
- It reads every other module in the module graph.
- It exports and opens all of its packages.
- It has no name, so no
requiresdirective can refer to it.
You can see the unnamed module for yourself:
public class WhereAmI {
public static void main(String[] args) {
Module mine = WhereAmI.class.getModule();
System.out.println(mine + " named=" + mine.isNamed());
System.out.println(String.class.getModule());
System.out.println(mine.canRead(java.sql.Connection.class.getModule()));
}
}javac -d classes WhereAmI.java
java -cp classes WhereAmIThe output looks like this. The hash after @ varies from run to run.
unnamed module @6d03e736 named=false
module java.base
trueThe one-way rule
Readability between named and unnamed modules runs in one direction only.
| From | To | Allowed? |
|---|---|---|
| Unnamed module | Named module (exported packages) | Yes, it reads every module in the graph |
| Named module | Unnamed module | No, it cannot be named in `requires` |
| Unnamed module | Non-exported package of a named module | No, strong encapsulation still applies |
| Named module | Another named module | Only through `requires` |
The second row is the one that causes trouble. A named module cannot depend on a plain jar that sits on the classpath. Declaring requires some.jar fails because there is no module of that name. Compiling against its packages fails with an error that the package does not exist or is not visible. The jar must either become a module (an automatic module, covered in the next chapter) or the dependency must be restructured.
Which modules classpath code can see
Classpath code can read any module that is part of the module graph. The graph is built from root modules, and the roots depend on how you start the program.
- When you start with
-cpand a main class, the roots are the system modules that export at least one package without qualification, such asjava.sql,java.xml, andjava.logging. This has been the rule since JDK 11. The unnamed module is added to the graph as well. - When you start with
-m, the main module is the root. Other modules enter the graph only throughrequires,--add-modules, or service binding. - Jars on the module path that nothing requires are not resolved unless you add them.
--add-modules ALL-MODULE-PATHmakes every module-path module a root.
Two consequences follow. First, a JDK module that was removed from the platform, such as java.xml.bind (removed in JDK 11), gives you a NoClassDefFoundError or compile error. You fix that by adding the library as a normal dependency. Second, a module-path module that no root requires can be silently absent. If classpath code expects it, add it with --add-modules.
Mixing both paths
A common setup keeps some jars on the module path and the rest on the classpath:
java --module-path mods --add-modules com.example.library \
-cp libs/legacy.jar:app-classes com.example.MainHere com.example.Main is in the unnamed module. It can use the exported API of com.example.library, but the library cannot declare a dependency on it.
Several rules apply to this mixed setup:
- Named modules win package lookups. If a package exists both in a named module and on the classpath, the classpath copy is ignored. Classes in it can never be loaded, even if the module lacks a class that the classpath copy has. This is the split-package problem from the unnamed side, and a later chapter covers it.
- Reflection ignores readability. A named module can still call
Class.forNameon a classpath class through the shared class loader, because core reflection does not check that the caller reads the target. Do not rely on this. It works around the module graph and breaks under other loaders. - You can add the edge by hand.
--add-reads com.example.app=ALL-UNNAMEDmakes a named module read the unnamed module. This is a migration and testing escape hatch, not a design.
javac --module-path mods --add-reads com.example.app=ALL-UNNAMED \
-cp libs/legacy.jar -d out --module-source-path src -m com.example.appEncapsulation applies to classpath code too
Classpath code gets no special access to JDK internals. In JDK 9 to 15, --illegal-access=permit was the default. It let classpath code reflect into most JDK packages that existed before Java 9, and printed one warning. Since JDK 16 the default became deny, and JDK 17 removed the option. Reflection into java.base internals now throws InaccessibleObjectException, unless you open the package for the unnamed module:
java --add-opens java.base/java.lang=ALL-UNNAMED -cp app.jar com.example.MainALL-UNNAMED is the special target name for the unnamed module. The same flag family includes --add-exports module/package=ALL-UNNAMED, which exposes only the public types and members, without deep reflection.
Services in the unnamed module
Classpath code uses services through the old mechanism: a provider file named META-INF/services/<interface name> inside a jar. ServiceLoader reads these files for providers in the unnamed module, and it also finds provides declarations from named modules. A named module that wants to load services must declare uses. Code in the unnamed module needs no declaration, because the unnamed module is treated as if it uses every service.
Common errors
| Symptom | Cause | Typical fix |
|---|---|---|
| `package x does not exist` when compiling a named module | The dependency is a plain classpath jar | Put the jar on the module path as an automatic module |
| `InaccessibleObjectException: ... does not "opens java.lang" to unnamed module` | Classpath code reflects into a JDK package | Upgrade the library, or add `--add-opens ... =ALL-UNNAMED` |
| `NoClassDefFoundError` for a `javax.*` class | The JDK module was removed | Add the replacement library as a dependency |
| A class on the classpath is never loaded | Its package is also in a named module | Remove the duplicate, or fix the split package |
Exercise
A modular application (com.example.app) must call a library that you only have as libs/util.jar, and you start the program with -m. You put util.jar on -cp. What two things stop it from working cleanly, and what is the standard fix?
Show answer
First, com.example.app is a named module and cannot read the unnamed module, so it cannot declare requires for the jar and the compiler will not see its packages. Second, with -m the classpath jar is not a root dependency the module can name. The standard fix is to put util.jar on the module path so it becomes an automatic module with a name derived from its file name or Automatic-Module-Name, and add requires util; to the descriptor. --add-reads com.example.app=ALL-UNNAMED is only a temporary workaround.
Exercise
What does Foo.class.getModule().isNamed() return for a class loaded from -cp classes, and what does String.class.getModule().getName() return?
Show answer
The first returns false, because classpath classes live in the unnamed module. The second returns "java.base".