Java 9 Jlink

What does the Java 9 JLink tool do

The java 9 JLink tool enables you to create a runtime application by assembling a specific set of modules. With this tool, Java 9 now has the capability to create an application that works even if you don’t have the JRE installed. JLink creates a minimal JRE required to run your application. It allows optimizing the runtime for the modules that you have included.

What are the advantages of Java 9 JLink tool

The JLink tool adds new functionalities and also opens up a few possibilities. Here are some of the advantages.

  1. The runtime image can be optimized since it is possible to optimize the custom application module along with the platform modules.
  2. Since the runtime created by jLink contains only the required modules, the size of the module is small. This opens up the possibility to use the application inside embedded and IOT devices.
  3. The user does not have to install the JRE
  4. Since the application image contains a subset of modules, it is safer from a security point of view since there would be fewer vulnerabilities

What are the disadvantages of Java 9 JLink tool

There are a few caveats to using the JLink tool.

  1. The application image once created, cannot be updated or patched. For any changes, a new application needs to be deployed
  2. When the user updates the JRE on her machine, the runtime image created by JLink does not get those updates and so any security fixes will not be applied.

Let us now look at an example of how to create a runtime image using JLink. We will create an application from the modules that we created as part of the modules tutorial here . We created the HelloWorldModule and HelloWorldModuleHelper that together take in a string and print “Hello World ” on the console. Lets take both the modules and built a runtime image out of it.

The image above shows how the two modules look like. To create the runtime image we use this command :

Java 9 Modules
Java 9 Modules

The jlink binary is inside the bin folder of the jdk.
–module-path specifies the path of the modules. In our example, we specify the path to the jvm modules and the path to our modules (.) separated by :
–add-modules tells the tool which modules should be the part of the runtime image. The tool adds the modules specified here plus all the modules that are required by this module recursively.
–launcher tells the tool that we need a launch script that will launch the runtime. If not specified, the tool will only create a java executable and we need to run the main class.
–output specifies the output directory where the runtime image would be created.
Once you run the command this is how the output directory looks like

Jlink output
Jlink output

To run the application cd to the “Hello” directory and type in “./bin/launch StudyTrails”. This will print “Hello World StudyTrails” on the console.

To summarize, JLink provides an optimal way to create a minimal runtime environment that can run with any external JRE. Its most promising use would be in IOT and embedded devices.

Leave a Comment