Sub topic 16
Migrating an existing application
This chapter assumes you know how module-info.java, requires, exports and opens work. It also assumes you have run java with --module-path, and that you know what a jar and the classpath are. The goal is to move a classpath application to modules in small steps, with the application working after each step.
Start from the fact that nothing has to change
An application can stay on the classpath indefinitely. Code on the classpath goes into the unnamed module, which reads every other module and exports and opens all of its packages. Migration is optional per jar, and you can do it gradually.
The cost of staying is that the JDK itself is encapsulated. Since JDK 16, internal packages such as sun.misc or java.base/java.lang are closed to reflection by default, even for classpath code. Most forced migration work comes from this, not from your own code.
The three kinds of module you will mix
| Kind | Where it lives | Reads | Exports |
|---|---|---|---|
| Named module | Module path, has `module-info.class` | Only what it `requires` | Only what it `exports` |
| Automatic module | Plain jar on the module path | All modules, including the unnamed module | Every package |
| Unnamed module | Classpath | All modules | Every package |
A named module cannot declare requires on the unnamed module, so it can never use classpath code directly. This is the rule that forces the order of migration.
Choose a direction
The dependency graph decides the order. Two strategies work:
- Bottom-up. Convert the libraries with no dependencies first, then move up toward the application. Each converted jar can be a named module on the module path while the rest stays on the classpath. This is the clean approach when you own all the code.
- Top-down. Put the application's own jars on the module path as automatic modules first. Then add descriptors from the top, starting with the application, and replace automatic modules with real ones as the libraries release them.
Step 1: analyze with jdeps
Run jdeps before you change anything. It reports which packages and JDK modules each jar uses, and it flags internal APIs.
# Which JDK internal APIs does the code use, and what replaces them?
jdeps --jdk-internals app.jar
# Which JDK modules does the app need?
jdeps --print-module-deps --ignore-missing-deps app.jar
# Draft a descriptor for a library jar (writes to the directory "generated")
jdeps --generate-module-info generated libs/legacy-lib.jarTreat the generated module-info.java as a draft. jdeps sees only static references, so it will miss Class.forName, reflection-based frameworks and service lookups.
Step 2: handle automatic modules
When a plain jar is on the module path, the runtime makes it a module. The name comes from the Automatic-Module-Name manifest entry if present. Otherwise the runtime derives it from the file name: legacy-lib-1.2.jar becomes legacy.lib. The version suffix is stripped and non-alphanumeric characters become dots.
A derived name is fragile, because renaming the jar renames the module. Library authors should therefore set a stable name in the manifest, even before they add a full descriptor:
Automatic-Module-Name: com.acme.legacyjar --create --file legacy-lib.jar --manifest MANIFEST.MF -C classes .Once the name is stable, other modules can write requires com.acme.legacy;. If you ship a library, publishing this entry is the cheapest step you can take to help your users.
module com.example.app {
requires com.acme.legacy; // automatic module from the module path
requires java.sql;
}Automatic modules also bring a trap. An automatic module reads the unnamed module, so it can still see classpath jars. If you move that classpath code to the module path later, the module graph changes and results can differ.
Step 3: fix split packages
A split package is one package name that appears in two modules that are both resolved. The module system rejects this, because each package must belong to exactly one module. A typical failure message looks like this:
Error occurred during initialization of boot layer
java.lang.LayerInstantiationException: Package com.acme.util in both module a and module bCommon causes are two jars from the same vendor sharing a package, and a library that re-packages the code of another library. Your options, in order of preference:
- Merge the two jars into one module.
- Rename the package in one of them.
- Keep one of the jars on the classpath, where split packages are allowed because the unnamed module has no such rule.
- For testing or patching only, use
--patch-moduleto add classes into an existing module.
Step 4: fix reflection
Frameworks such as serializers, dependency injection containers and ORMs call setAccessible(true) on private members. Against a named module this fails unless the package is open. You see this error:
java.lang.reflect.InaccessibleObjectException: Unable to make field private int com.example.model.Person.age accessible:
module com.example.app does not "opens com.example.model" to unnamed moduleFix it in your own module with an opens directive. For JDK packages or jars you do not control, use a command-line flag as a stopgap:
java --add-opens java.base/java.lang=ALL-UNNAMED -cp app.jar com.example.Main--add-opens is acceptable as a documented, temporary bridge. Track each one and remove it when the library is updated.
Step 5: fix missing modules
Java 11 removed the Java EE and CORBA modules, for example java.xml.bind and java.activation. Code that used them compiles on JDK 8 and fails later with a ClassNotFoundException or a FindException: Module not found. Add the standalone replacement artifact (such as Jakarta XML Binding) as a dependency. Do not look for a JDK flag to bring the module back, because the modules are gone from the JDK.
A named module also sees only the JDK modules it requires. Add the missing requires java.sql; or requires java.logging; as the compiler reports them.
Finish with a runtime image
Once the application and its dependencies are named modules (automatic modules are not accepted by jlink), you can build a small runtime. The jdeps --print-module-deps output tells you which JDK modules to include, and jlink --add-modules takes it from there.
Exercise
Your application, com.example.app, is a named module. It must use reports-2.3.jar, a third-party jar with no descriptor and no Automatic-Module-Name. You put the jar on the module path. What module name does it get, what do you write in module-info.java, and why is the placement of the jar on the classpath instead not an option here?
Show answer
The runtime derives the name from the file name, so the module is reports: the -2.3 version suffix is dropped. You write requires reports;. Putting the jar on the classpath would place it in the unnamed module, and a named module cannot requires the unnamed module, so com.example.app could not use its classes. You should ask the vendor to publish an Automatic-Module-Name, because the derived name changes if the jar is renamed.
Exercise
After moving two jars to the module path, startup fails with "Package com.acme.util in both module a and module b". Name two ways to resolve it.
Show answer
Merge the two jars into one module, or rename the package in one of them. Keeping one jar on the classpath also works, because the unnamed module is not subject to the one-package-one-module rule.