Sub topic 13
Working with non-modular code
This chapter assumes you can build a modular application and run jlink on it, as shown in the earlier chapters. Real projects rarely stay that tidy. They depend on JARs that have no module-info.class, such as older libraries, internal utilities, and vendor SDKs. jlink is strict about those JARs, so you need a plan for them.
Why jlink rejects plain JARs
jlink links only explicit modules, meaning modules that have a module-info.class. A JAR without a descriptor is not a module until you put it on the module path. There it becomes an automatic module.
An automatic module has these properties:
- Its name comes from the
Automatic-Module-Namemanifest entry. If that entry is missing, the name comes from the file name. The version suffix is removed and every non-alphanumeric run becomes a dot, solegacy-greet-1.2.jarbecomeslegacy.greet. - It exports all of its packages.
- It reads every other module, including the unnamed module that holds the classpath.
The last property is the problem. An automatic module can depend on anything, but nothing declares what. jlink cannot work out a closed set of modules from that, so it refuses with an error like this:
Error: automatic module cannot be used with jlink: legacy.greet from file:///.../libs/legacy-greet.jarYou have two strategies, summarized below.
| Strategy | What you do | Result | Best when |
|---|---|---|---|
| Make the JAR a real module | Generate and add a `module-info.class` | The library is linked into the image | You need a fully self-contained runtime |
| Link only the JDK | Put the app and libraries on the classpath and run them with the image's `java` | The image holds JDK modules only | The dependency tree is large or unmodular |
A worked example
You will build a tiny non-modular library, use it from a modular application, watch jlink fail, and then fix it. Everything runs from one directory.
Build the legacy library
Create legacy-src/com/legacy/greet/Greeter.java:
package com.legacy.greet;
public class Greeter {
public static String greet(String name) {
return "Hello, " + name;
}
}Compile it into a JAR with no descriptor:
mkdir -p lib-classes libs
javac -d lib-classes legacy-src/com/legacy/greet/Greeter.java
jar --create --file libs/legacy-greet.jar -C lib-classes .Build the modular application
Create src/module-info.java. The name after requires is the automatic module name derived from the file name:
module com.example.app {
requires legacy.greet;
}Create src/com/example/app/Main.java:
package com.example.app;
import com.legacy.greet.Greeter;
public class Main {
public static void main(String[] args) {
System.out.println(Greeter.greet("jlink"));
}
}Compile it and package it. The application runs fine on the module path, because automatic modules are allowed there:
mkdir -p out/com.example.app mods
javac --module-path libs -d out/com.example.app \
src/module-info.java src/com/example/app/Main.java
jar --create --file mods/app.jar \
--main-class com.example.app.Main \
-C out/com.example.app .
java --module-path mods:libs -m com.example.appWatch jlink fail
jlink \
--module-path "$JAVA_HOME/jmods:mods:libs" \
--add-modules com.example.app \
--output build/runtimeThis stops with the "automatic module cannot be used" error shown earlier. No partial image is produced.
Strategy 1: turn the JAR into a module
jdeps can write a module-info.java for a JAR by analyzing which packages the JAR uses and which it contains:
jdeps --generate-module-info gen libs/legacy-greet.jar
cat gen/legacy.greet/module-info.javaThe generated file for this library looks like this:
module legacy.greet {
requires java.base;
exports com.legacy.greet;
}Compile that descriptor against the original JAR, then add the resulting class to a copy of the JAR:
mkdir -p patch modular-libs
javac -d patch --patch-module legacy.greet=libs/legacy-greet.jar \
gen/legacy.greet/module-info.java
cp libs/legacy-greet.jar modular-libs/
jar --update --file modular-libs/legacy-greet.jar -C patch module-info.classNow point jlink at the patched copy instead of libs:
jlink \
--module-path "$JAVA_HOME/jmods:mods:modular-libs" \
--add-modules com.example.app \
--launcher hello=com.example.app/com.example.app.Main \
--strip-debug --no-header-files --no-man-pages \
--output build/runtime
build/runtime/bin/java --list-modules
build/runtime/bin/helloThe module list should include com.example.app, legacy.greet, and java.base. The launcher prints Hello, jlink.
Rules for generated descriptors
- Treat the output of
jdeps --generate-module-infoas a draft. It exports every package in the JAR. Remove exports that are internal. - If the library depends on other non-modular JARs, pass them all with
--module-path.jdepsthen writesrequireslines between them. Each JAR needs its own patched copy. - Do not modify the original JAR. Keep the patched copies in a separate directory, such as
modular-libs, so you can regenerate them when you upgrade the library. - Check whether the library publishes a modular version first. Many projects now ship a real descriptor, which removes this whole step.
Strategy 2: link the JDK only
If the dependency tree is large, or some JARs can't be patched, give up on linking the libraries. Build an image that contains only the JDK modules the code needs, then run the application on the classpath with that image's java.
Ask jdeps for the JDK modules across the application and its libraries:
MODS=$(jdeps --ignore-missing-deps --print-module-deps \
--class-path 'libs/*' mods/app.jar)
echo "$MODS"Build the image from that list:
jlink \
--module-path "$JAVA_HOME/jmods" \
--add-modules "$MODS" \
--strip-debug --no-header-files --no-man-pages \
--output build/jdk-onlyRun the application, using the image's java and the classpath:
build/jdk-only/bin/java -cp "mods/app.jar:libs/*" com.example.app.MainThe -cp flag puts everything in the unnamed module, so the descriptor of app.jar is ignored. Library and application updates no longer require a new image. The image changes only when the JDK modules you need change.
Gotchas
--ignore-missing-deps. Without it,jdepsstops at the first class whose dependency is not on the class path. With it, the module list can silently miss modules. Check that the result looks plausible.- Reflection and service loading.
jdepsreads bytecode references. It cannot seeClass.forName("...")with a computed name, or a JDBC driver found throughServiceLoader. Add such modules yourself with--add-modules. Typical cases arejava.sql,jdk.localedatafor non-English locales, and, before JDK 22,jdk.crypto.ecfor some TLS cipher suites. - Multi-release JARs. The module list can depend on the Java version
jdepsanalyzes. Pass--multi-releasewith your target version when a library ships versioned classes. - Wrong names. The name in
requiresmust match the module namejdepsgenerates. If you rename a JAR, the derived automatic name changes with it. - Split packages. Two modules cannot contain the same package. Libraries that were fine on the classpath may break once both are modules.
Exercise
A build fails at the jlink step with "automatic module cannot be used with jlink" for a third-party JAR. Name two ways to get a working image, and state one cost of each.
Show answer
Option 1: generate a module descriptor with jdeps --generate-module-info, compile it with javac --patch-module, add module-info.class to a copy of the JAR with jar --update, and put the patched copy on the jlink module path. The cost is that you maintain the patched JAR and must redo it for each library upgrade. Option 2: build an image of only the JDK modules, found with jdeps --print-module-deps, and run the application on the classpath with the image's java -cp. The cost is that the image does not contain the libraries, so you must ship and update them separately, and the module system no longer enforces encapsulation for them.