Sub topic 13
Working with non-modular code
This chapter assumes you can build an image with jlink from a modular application and read jdeps output. It covers what happens when your application depends on JARs that have no module-info.class.
Why jlink rejects plain JARs
jlink only accepts explicit modules. A JAR with no module descriptor becomes an automatic module when you place it on the module path. Its name comes from the Automatic-Module-Name manifest entry, or from the file name if that entry is missing. Automatic modules can read every other module and export every package, so jlink cannot work out what they need. If you add one to the image, it stops with an error saying the automatic module cannot be used.
A JAR on the classpath is not a module at all, so jlink never sees it.
Choose a strategy
Option 1: Link only the JDK modules and keep the classpath
This is the least invasive approach. The image holds the JDK modules your code and libraries need, and the application JARs stay outside it.
MODS=$(jdeps --print-module-deps --ignore-missing-deps \
--class-path 'libs/*' mods/app.jar)
jlink --add-modules "$MODS" \
--strip-debug --no-header-files --no-man-pages \
--output build/runtime
build/runtime/bin/java -cp "mods/app.jar:libs/*" com.example.app.Main--ignore-missing-deps stops jdeps from failing on optional dependencies that are not present. The cost is that your application is no longer inside the image. You ship the image and the JARs together.
Option 2: Give the library a module descriptor
If you can modify a copy of the library, ask jdeps to write a descriptor and add it to the JAR. The generated name comes from the file name, so legacy-lib.jar becomes legacy.lib.
cp libs/legacy-lib.jar mods/legacy-lib.jar
jdeps --generate-module-info out/modinfo mods/legacy-lib.jar
javac --patch-module legacy.lib=mods/legacy-lib.jar \
-d out/classes out/modinfo/legacy.lib/module-info.java
jar uf mods/legacy-lib.jar -C out/classes module-info.classAfter this, mods/legacy-lib.jar is an explicit module. Add requires legacy.lib; to your own descriptor and put mods on the jlink module path. Dependencies of that library that are also non-modular need the same treatment.
Gotchas
jdepsreads bytecode. It misses modules reached by reflection orServiceLoader, such as JDBC drivers needingjava.sqlorjava.naming. Add these with extra--add-modulesentries.- A generated descriptor is a starting point. Check its
requireslines, and expect anexportsline for every package. - Split packages (the same package in two modules) are an error on the module path. Libraries that repeat packages from other JARs must be fixed or left on the classpath.
- Patching a third-party JAR means you maintain that patch with every library upgrade.
Exercise
Your application uses three non-modular libraries and you do not want to patch any of them. How do you build the image, and how do you run the application?
Show answer
Run jdeps --print-module-deps --ignore-missing-deps --class-path 'libs/*' on your application JAR to get the JDK modules, and pass the result to jlink --add-modules. Run the app with the image's bin/java -cp and the application and library JARs on the classpath.