(Using jdk14)
Starting by the fact that in order to use jlink
your main jar should be a module.
How? Consider that you have a maven
project. You just need to include module-info.java
inside src/main/java
dir and make sure that you require
the modules that your app needs and you export
the package that contains your main class. In most cases you will get a compile-time error when missing a requires
. Have in mind that non-modular dependencies become automatic modules.
You can use maven's copy-dependencies
to make sure that all dependencies are copied under target/lib
during mvn package
.
next step: jlink
Since jlink
maven plugin is still in alpha
, you can use command-line.
NOTES:
jlink
will create a self-contained bundle directory that contains
- main app module
- app dependencies
- jdk required modules
- app launcher (optional)
jlink
bundle targets one platform at a time. By default it is the current platform.
javafx
runtime modules are also platform-specific. But since they are not part of the jdk we need to always provide the module-path containing them.
javafx
runtime modules can be downloaded from web, or from maven repo by using the corresponding target platform classifier
(win/linux/mac).
jlink
can also create cross-platform bundles. Just include the target platform modules to the --module-path
(e.g. from linux: download windows jdk
/ javafx
and add their jmods
dirs to module-path
).
jlink command
Case 1: build and target platforms are the same
NOTE: /path-to/javafx-mods
needs to be provided to your modulepath
unless you copy the required javafx deps under lib/
using maven (copy-dependencies).
jlink --launcher run=jdk14Example/com.example.javafx.app.Main \
--module-path ./lib:javafx-jdk14-example-1.0.0.jar:/path-to/javafx-mods \
--add-modules=jdk14Example --output app-bundle
Case 2: build and target platforms are differrent
# Building from linux for windows
jlink --launcher run=jdk14Example/com.example.javafx.app.Main \
--module-path ./lib:javafx-jdk14-example-1.0.0.jar:/path-to/jdk-win/jmods:/path-to/javafx-mods-win \
--add-modules=jdk14Example --output app-bundle
Conclusion:
In both of the above cases you get a directory with a self-contained application which can run on a workstation with no java
/javafx
installed.
# if jlink targeted linux
app-bundle/bin/run
# if jlink targeted windows
app-bundle/bin/run.bat
# if jlink targeted mac
app-bundle/bin/run