What is the JLink tool in Java 9?

JLink is a new linker tool that has been used to create our own customized JRE. Usually, we can run our program using default JRE provided by Oracle. If we need to create our own JRE then use this tool. JLink tool can help to create its own JRE with only the required class to run the application. It can reduce the size of the API developed and the dependency of using full JRE.

In Java 9, we have a new phase between compiling the code and its execution link time. Link time is an optional phase between the phases of compile-time and runtime.

Command to create custom JRE

jlink --module-path --add-modules --limit-modules --output
  • module-path is the path where observable modules have discovered by the linker. It can be modular JAR files, JMOD files, and modules.
  • add-modules names the modules to add to the run-time image, these modules can, via transitive dependencies, cause additional modules to be added.
  • limit-modules limits the universe of observable modules.
  • the output is the directory that contains the resulting run-time image.

jlink --module-path $JAVA_HOME/jmods:mlib --add-modules com.greetings --output greetingsapp

In the above command, the value to module-path is a PATH of directories containing the packaged modules. JAVA_HOME/jmods is a directory containing java.base.jmod, other standards, and JDK modules. The directory mlib on the module path containing the artifact for module com.greetings.