Learnitweb

The guide to jlink in Java

1. Introduction

The jlink tool is used to link a set of modules, along with their transitive dependencies, to create a custom runtime image. The jlink enables you to generate a custom runtime image that contains only the required platform modules for an application.

You can use the jlink tool to assemble and optimize a set of modules and their dependencies into a custom runtime image.

In this tutorial, we’ll learn how to use jlink to create a custom runtime image.

2. Need to create custom runtime images

Until Java 1.8, to run a small and simple program, you need all the predefined classes (4300+) in the JRE. This increases the size of the Java Runtime environment and Java applications. Due to this reason Java is not suitable for IOT devices and microservices.

To understand the need for custom runtime image, let us write a simple Java program:

public class HelloWorld {
    private static final Logger LOG = Logger.getLogger(HelloWorld.class.getName());
    public static void main(String[] args) {
        LOG.info("Hello World!");
    }
}

To run this program we need only HelloWorld, String, Logger, and Object classes. Though only four classes are used, there is no way to specify this and the entire JRE with all its shipped classes is used tun run this small and simple program.

Therefore, to run a small program, a complete JRE is required, which is a waste of memory. This increases the size of the Java Runtime environment and Java applications. Due to this reason Java is not suitable for IOT devices and microservices.

Due to this reason a customized JRE was required. Using jlink, you can create your own customized JRE which contains only the required classes to run the application.

3. Example of jlink

To understand the use and significance of jlink, let us first write a simple program with module and run without jlink.

In this example, we’ll write a simple program in a module myModule. Following is the file structure:

src
└───myModule
    │   module-info.java
    │   
    │
    └───mypack
            Test.java

Following is the code of Test.java

package mypack;
public class Test {
	public static void main(String[] args) {
		System.out.println("hello from jlink");
	}
}

Following is the module-info.java.

module myModule {

}	

Following is the command to compile the program:

javac --module-source-path src -d out -m myModule

Following is the command to run the program after compilation:

java --module-path out -m myModule/mypack.Test

The output of the program is:

hello from jlink

4. Using jlink to create custom runtime image

Let us now create a custom runtime image for the program we wrote earlier.

Our myModule requires java.base module. Therefore add java.base module to out directory. The java.base module can be found in the bin directory of you JDK installation.

src
│   java.base.jmod
│
└───myModule
    │   module-info.class
    │
    └───mypack
            Test.class

Now we can create our own custom runtime image using jlink using the following command:

jlink --module-path out --add-modules myModule,java.base --output customjre

Now, we can use the following command to run the program:

D:\Java\customjre\bin>java -m myModule/mypack.Test

Notice the current directory while running the custom runtime image. You are in the bin directory of your runtime image.

5. Compressing the size of custom runtime image with compress plugin

If you check the size of the customjre we created in previous example, it will be around 40 MB which is much less that the Java JRE. We can further reduce the size using compress plugin.

If you run the jlink -- help, you can see the compress option.

  -c, --compress=<0|1|2>                Enable compression of resources:
                                          Level 0: No compression
                                          Level 1: Constant string sharing
                                          Level 2: ZIP
      --disable-plugin <pluginname>     Disable the plugin mentioned
      --endian <little|big>             Byte order of generated jimage
                                        (default:native)

You can use the following command to compress the customjre:

jlink --module-path out --add-modules myModule,java.base --compress 2 --output compressedjre

6. Providing name to the application using launcher

You can provide a name to your application, instead of typing the entire command:

D:\Java>jlink --module-path out --add-modules myModule,java.base --launcher myapp=myModule/mypack.Test --compress 2 --output newCompressedJre

You can now run the application like the following:

D:\newCompressedJre\bin>myapp

7. Conclusion

By leveraging jlink’s modularization and linking capabilities, developers can optimize their application’s footprint, enhance its performance, and simplify deployment across different platforms.

Hope this article helped.