Sub topic 9
Adding a launcher
This chapter assumes you can build an image with jlink and can read a module-info.java file. Both were covered in the previous chapters.
Why a launcher helps
Without a launcher, you start the application from the image like this:
build/runtime/bin/java -m com.example.app/com.example.app.MainThat command is long and exposes module and class names to the user. A launcher is a small script that jlink generates in the image's bin directory. It runs that command for you and passes along any arguments.
The --launcher option
The option has this form:
--launcher <name>=<module>[/<main-class>]nameis the file name of the script inbin.moduleis the module that contains the entry point.main-classis the class withmain. You can leave it out if the module records a main class.
Our JAR was built with --main-class com.example.app.Main, so the main class is recorded in the module descriptor. Both of these forms work for it:
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--launcher hello=com.example.app/com.example.app.Main \
--output build/runtime
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--launcher hello=com.example.app \
--output build/runtime-shortThe first form names the class explicitly. The second reads it from the JAR. Run the commands with different --output directories, because jlink refuses to write into a directory that already exists.
What jlink generates
On Linux and macOS, bin/hello is a shell script. On Windows it is a .bat file. The Unix version looks like this:
#!/bin/sh
JLINK_VM_OPTIONS=
DIR=`dirname $0`
$DIR/java $JLINK_VM_OPTIONS -m com.example.app/com.example.app.Main "$@"The script calls the java next to it, so it always uses the image's own runtime and never a JDK found on the PATH. The "$@" passes your command-line arguments to main.
Several launchers and default JVM options
You can repeat --launcher to create one script per entry point, such as a server and an admin tool. Each name must be unique.
To give a launcher default JVM options, add --add-options at link time. The options are written into the JLINK_VM_OPTIONS line of every launcher in the image:
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--launcher hello=com.example.app \
--add-options="-Xmx256m" \
--output build/runtime-optsCommon errors
- Module not found. The module in
--launchermust be resolvable from--module-pathand part of the image. Naming a module that is not in--add-modulesor reachable from it makesjlinkfail. - No main class. Using
hello=com.example.appon a JAR built without--main-classfails. Add the class after a slash. - Wrong separator. The launcher uses
=between name and target and/between module and class. A.or:there produces an invalid argument error. - Windows scripts. The launcher is a
.batfile, so run it ashello.batorhellofromcmd. A Linux-built image will not run on Windows.
Exercise
You built a modular JAR without --main-class. Which --launcher argument starts com.example.tools.Cli in module com.example.tools under the command name tool?
Show answer
--launcher tool=com.example.tools/com.example.tools.Cli. The class name is required after the slash because the module descriptor records no main class.
Exercise
Where in the image does jlink place the launcher, and why does it keep working when another JDK is installed on the machine?
Show answer
It places the script in the image's bin directory. The script calls the java that sits beside it, so it uses the image's own runtime instead of any JDK on the PATH.