Sub topic 9
Adding a launcher
This chapter assumes you can build an image with jlink as in the previous chapters. It also assumes you know what a module descriptor is and what --add-modules does. You will see how --launcher creates a command in the image's bin directory, what that command contains, and what can go wrong.
Why a launcher helps
An image built without --launcher already runs your program. You call the image's own java and name the module:
build/runtime/bin/java -m com.example.app/com.example.app.MainUsers have to know the module name and the main class, and they have to type both every time. A launcher replaces that command with a short name such as hello. You choose the name when you build the image.
The `--launcher` option
The option has this form:
--launcher <name>=<module>[/<main-class>]The command from the first jlink image already used it:
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--launcher hello=com.example.app/com.example.app.Main \
--strip-debug --no-header-files --no-man-pages \
--output build/runtimeThe part before = is the file name that appears in bin. The part after it names the module and, optionally, the main class.
| Form | Meaning | When it works |
|---|---|---|
| `hello=com.example.app/com.example.app.Main` | Module plus an explicit main class | Always, if the module is in the image |
| `hello=com.example.app` | Module only | Only if the module records a main class |
A module records its main class when you package it with jar --main-class, as the earlier build step did. The JAR then stores the class in the module descriptor. If you omit the class and none is recorded, jlink stops with an error. Naming the class explicitly is the safer habit, and it lets you override the recorded one.
What jlink generates
After the build, list the bin directory:
ls build/runtime/binOn Linux or macOS you see the JDK tools that the selected modules provide, such as java and keytool if java.base includes them, plus a file named hello. The hello file is a small shell script. Its contents look like this:
#!/bin/sh
JLINK_VM_OPTIONS=
DIR=`dirname $0`
$DIR/java $JLINK_VM_OPTIONS -m com.example.app/com.example.app.Main "$@"The exact text can differ slightly between JDK versions, but the structure is stable. The script does three things:
- It finds its own directory, so the image works from any location.
- It calls the
javanext to it, not ajavafrom yourPATH. - It passes
-m module/classand forwards all of your command-line arguments with"$@".
On Windows, jlink writes hello.bat instead, with the same logic written for cmd.
flowchart LRThe diagram below shows the path from the command you type to your code.
The launcher is only a convenience script. It adds no new runtime behavior. Anything the script does, you can do by hand with bin/java -m.
Running it
build/runtime/bin/helloThe program prints a log line from java.util.logging, such as INFO: Hello from a custom runtime. Arguments pass straight through:
build/runtime/bin/hello one twoYour main receives args containing one and two.
Several launchers in one image
You can repeat the option. Each launcher gets its own script:
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--launcher hello=com.example.app/com.example.app.Main \
--launcher hello-debug=com.example.app/com.example.app.Main \
--output build/runtimeTwo names pointing at the same class is not very useful by itself. It becomes useful when different modules or different main classes provide separate tools, for example a server and an admin command in one image. Each launcher module must be part of the image, either named in --add-modules or pulled in as a dependency of a named module. If it is not, the build fails.
Passing JVM options
The generated script has a variable called JLINK_VM_OPTIONS, which is empty by default. Setting it changes what the launcher passes to java. Because the script assigns the variable itself on its first line, an environment variable of the same name does not carry through, so set JVM flags in one of these other ways:
- Call
bin/javadirectly with the flags you want, followed by-m module/class. - Bake default options into the image with
--add-options.
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--launcher hello=com.example.app/com.example.app.Main \
--add-options="-Xmx256m -Dapp.mode=prod" \
--output build/runtime--add-options stores the flags as defaults for the image's java command, so the launcher picks them up. Flags that you pass explicitly on the command line are processed after these defaults.
Common errors
The module is not in the image. The error message says the module cannot be found or is not resolved. Check that --add-modules names the launcher module and that --module-path contains it. The path must include mods as well as the jmods directory.
No main class. If you wrote hello=com.example.app and the JAR was built without --main-class, the build fails. Either add the class to the launcher spec or rebuild the JAR with --main-class.
Wrong class name. jlink checks only the module, not the class. A typo in the class name builds a working image whose launcher fails at run time with a message that the main class cannot be found. Always run the launcher once after the build.
Launcher not executable. On Unix, jlink marks the script executable. If you copy the image with a tool that drops permissions (some archive or CI cache steps do), run chmod +x build/runtime/bin/hello.
Spaces in paths. The Unix script invokes $DIR/java without quotes. An image placed in a directory whose path contains spaces can fail. Install images in paths without spaces, or call bin/java -m yourself with proper quoting.
When a launcher is not enough
A generated launcher has fixed behavior. If you need to compute options, read a configuration file, or set environment variables before the JVM starts, write your own wrapper script. The wrapper should call the image's bin/java -m ... or the generated launcher, using a path relative to the wrapper's own location. Keep the generated launcher as the stable entry point and put extra logic in the wrapper.
For a single self-contained installer with a native executable and an icon, the JDK has a separate tool, jpackage, which can build on top of a jlink image. That is a different step from the one covered here.
Checking your work
After each build, confirm three things:
ls build/runtime/binshows the launcher name you chose.cat build/runtime/bin/helloshows the module and class you expected.build/runtime/bin/helloruns and prints the expected output.
The third check catches the typo and permission problems listed above. The first two make it clear which launcher spec the image really contains when a build script generates the options for you.
Exercise
You build an image with --launcher tool=com.example.app (no main class). The build fails. The module's JAR was created with jar --create --file mods/app.jar -C out/com.example.app .. Give two ways to fix this.
Show answer
The JAR has no recorded main class, so jlink cannot infer one. Fix 1: rebuild the JAR with --main-class com.example.app.Main so the module descriptor records it. Fix 2: keep the JAR as it is and name the class in the launcher: --launcher tool=com.example.app/com.example.app.Main.
Exercise
A teammate says a launcher run after the build printed correct output, so the image must not depend on the machine's installed JDK. Which line of the generated script shows why this is likely true, and what could still make the claim false?
Show answer
The line $DIR/java ... calls the java in the image's own bin directory, found through dirname $0, not the java on the PATH. The claim could still be false if the test ran the program in a way that bypasses the launcher, such as a wrapper that calls plain java, or if the image was built without a module the program needs and a classpath fallback hid the problem. Running bin/java --list-modules confirms what the image contains.