Quiz: Java Runtime Images: jlink, Modular JARs and jdeps

Total: 6 questions

1. 

What is a jlink runtime image and how is it different from a JDK and a JRE?

A custom runtime image is a directory with the JVM, the java command and only the modules the application needs. It is built by the jlink tool. Modules make this possible: the JDK is split into about seventy modules, and jlink takes only the ones you need.

  • Full JDK: all platform modules and the tools (javac, jar, jlink), about 365 MB in the lesson example.
  • jlink image: the application modules and their dependencies, about 35-60 MB depending on options. There is no javac in it unless you add the jdk.compiler module.
  • JRE: a universal runtime with all platform modules; Oracle does not ship a separate JRE for Java 11 and later, and jlink images play that role.

An image is platform-specific: it runs only on the same OS and architecture as the JDK it was built from. The typical path from sources to an image: javac (classes and module-info.class) -> jar (a modular JAR) -> jdeps (which JDK modules are needed) -> jlink (the image). Where it helps: Docker images, desktop applications shipped with their own runtime, services that do not need the whole JDK.

2. 

What is a modular JAR, how do you record its main class and version, and how do the ways of running it differ?

A modular JAR is an ordinary JAR with module-info.class at its root (not in META-INF):

jar --create --file lib/billing.jar --main-class com.bill.Main --module-version 2.0 -C mods/com.bill .

--main-class (short forms -e and the letter e in jar cfe) records the main class both in the manifest and in the module descriptor. --module-version sets the version: jar --describe-module --file lib/billing.jar starts with the line com.bill@2.0 jar:file:///... and ends with main-class com.bill.Main.

  • java -p lib/billing.jar -m com.bill and java -p lib -m com.bill/com.bill.Main run it as a module; getModule().getName() returns com.bill.
  • java -jar lib/billing.jar and java -cp lib/billing.jar com.bill.Main run it from the class path: module-info.class is ignored, the code is in the unnamed module, and the module name is null.

Without --main-class the short forms fail: -m com.bill gives does not have a ModuleMainClass attribute, use -m <module>/<main-class>, and -jar gives no main manifest attribute.

3. 

What is jdeps for, and which of its options help when building an image?

jdeps analyzes bytecode and shows dependencies of modular and plain JAR files and of class directories. For module com.bill with requires java.sql:

  • jdeps -s lib/billing.jar - a summary: com.bill -> java.base, com.bill -> java.sql;
  • jdeps --print-module-deps lib/billing.jar - java.base,java.sql, a comma-separated list in exactly the format jlink --add-modules expects;
  • jdeps --list-deps lib/billing.jar - the same modules, one per line;
  • --jdk-internals - finds uses of JDK internal APIs (for example, sun.misc.Unsafe from jdk.unsupported) and suggests a replacement;
  • --generate-module-info - creates a draft module-info.java for a plain JAR, the first step in moving a library to modules.

Only direct dependencies are shown: java.logging and java.xml, which java.sql requires transitively, are not in the list; jlink adds them itself. Classes loaded by name through reflection are invisible to jdeps.

4. 

How do you build an image with jlink, what is inside it, and which options make it smaller?

The minimal command: jlink --module-path lib --add-modules com.bill --output img. --add-modules names the root modules; jlink finds everything else (including transitive dependencies) itself. JDK modules are taken from the jmods directory of the installed JDK automatically.

The image is an ordinary directory: bin (java), conf, include (JNI headers), legal, lib (modules in the lib/modules file) and the release file with the JAVA_VERSION and MODULES lines. Run it with img/bin/java -m com.bill, without -p; java.home points to the image directory.

  • --launcher name=module - a launch script in bin (plus a .bat file on Windows), main class from the module descriptor; if there is none, the error is doesn't contain main class & main not specified in command line, and you need the name=module/class form;
  • --strip-debug - removes debug information;
  • --no-header-files, --no-man-pages - no include directory and no man pages;
  • --compress zip-0 ... zip-9 - resource compression; the old values 0, 1, 2 are deprecated;
  • --list-plugins - all plugins and their options.

In the lesson example these options shrank the image from about 59 MB to 35 MB.

5. 

Why does jlink have the --bind-services option, what does it cost, and what can you use instead?

Nobody requires service provider modules (provides ... with) through requires, so a plain jlink does not see them: the image builds, but ServiceLoader finds no provider in it.

--bind-services adds provider modules and their dependencies. The price: it adds providers of all services, including the JDK ones. In the lesson example the image grew from 3 to 50 modules and from about 49 MB to 162 MB, pulling in, for example, java.desktop and java.compiler.

That is why the required providers are more often listed explicitly: --add-modules com.shop.app,com.shop.card. Another option is --add-modules ALL-MODULE-PATH: every module on --module-path becomes a root, so all your own providers get into the image without extra JDK modules.

6. 

Which errors does jlink report most often, and how do you build an image for an application without module-info?

  • The --output directory already exists: Error: directory already exists: img. The image is not overwritten; delete the directory first.
  • No --add-modules: Error: no modules specified to --add-modules.
  • A plain JAR without module-info on --module-path: Error: automatic module cannot be used with jlink. jlink works only with explicit modules.
  • You need javac in the image: add jdk.compiler to --add-modules.
  • An image built on Windows does not run on Linux: it contains the native JVM of its own platform.

If a library is not modular, you can wait for a modular version, create a module-info for it (jdeps --generate-module-info gives a draft), or build an image from JDK modules only and run the application from the class path:

jdeps --print-module-deps app.jar
java.base,java.sql
jlink --add-modules java.base,java.sql --output rt
rt/bin/java -jar app.jar

A forgotten module shows up only at run time (NoClassDefFoundError: java/sql/Connection in an image with only java.base), so take the list from jdeps. jdeps does not see the modules of classes loaded by name through reflection; add them by hand.

Page 1 of 1