Jlink in Java 9
Jlink is Java’s new command line tool through which we can create our own customized JRE.
Usually, we run our program using the default JRE, but in case if you want to create your own JRE, then you can go with the jlink concept.
Why build your own JRE?
Let’s look at an example.
Suppose we have a simple “hello world” program like:
class Test {
public static void main(String[]args) {
System.out.prinltn("Hello World");
}
}
If I want to run this small program on our system, I need to install a default JRE. After installing the default JRE, I can happily run my small “hello world” application.
The Problem
To execute this small “hello world” application, we require the following .class files:
- Test.class
- String.class
- System.class
- Object.class
Here, these 4 .class will be enough to run my application.
The default JRE provided by Oracle contains 4300+ predefined Java .class files.
If I execute my “hello world” application with the default JRE, then all the predefined .class files will be executed. But if I only need 3-4 .class files to execute my “hello world” application, then why I need to maintain the outer .class files?
So the problem with the default JRE is that it executes the all predefined .class files whether you want to or not.
And if you also look into the default JRE's size then you will find that it is 203 MB. For executing my simple 1 KB of code, I have to maintain 203 MB of JRE in my machine. It is a complete waste of memory.
So using the default JRE means:
- Waste of memory and a performance hit
- Will not be able to develop microservices that contain very little memory.=
- Not suitable for IoT devices
So Java was not the best choice for microservices and IoT devices, but that was only a problem through Java 1.8. Meanwhile, Java 1.9 comes with jlink. With jlink, we can create our own small JRE that contains the only relevant class(es) that we want to have. There won't be a waste of memory, and performance will increase.
Jlink allows us to link sets of only required modules to create a runtime image (our own JRE)
Creating Our JRE With Required Modules
Suppose my “hello world” program is in a module named DemoModule. We can compile our module-based application in Java 9:
javac –module-source-path src -d out -m demoModule
After compiling successfully, a folder with a Test.class file will be created. If you run this module-based application using the default JRE, you can use the command:
java –module-path out -m demoModule/knoldus.Test
But as we discussed, our “hello world” program required only a few .class files — String.class, System.class, and Object.class. These .class files are part of the java.lang package, and the java.lang package is part of the java.base module. So, if I want to run my “hello world” program, only two modules are required – DemoModule and the java.base module. With these two modules, we can create our own customized JRE to run this application.
You can find the java.base module in the path:
java\jdk-9\jmods
So just copy the java.base module and paste it into the folder that has the Test.class file. Now we can create our own JRE by using the command:
jlink –module-path out –add-modules demoModule,java.base –output myjre
After executing this command successfully, you will find there is a myjre folder, which is nothing but your customized JRE. Just follow a few steps to execute your program by using the customized JRE
-
cd myjre
-
cd bin
-
java -m demoModule/knoldus.Test
By executing these commands, you can happily run your “hello world” application. That is all for jlink! I hope you now have a clear picture of how to use it to make your own JRE.
This article first appeared on the Knoldus blog.