JLink | Java Linker

18 Nov, 2021
In a single sentence, we can say that Jlink is used to create our own customized small JRE(Java RunTime Environment). JLink is the Java’s new command line tool(available in JDK_HOME/bin) which allows us to link sets of only required modules (and their dependencies) to create runtime environment (our own JRE). Usually, we use the default JRE for running our programs which are provided by the Oracle Corporation, but if we want our own JRE like geeks jre then we can go for JLINK. 
Suppose you have written a small program for adding two numbers. Now it’s a very basic program in java. We can run our program with default JRE.  
Java
public class Practice {    public static void main(String[] args)    {        System.out.println(10 + 5);    }}
To run this program, it will use a Practice.class file, a String.class file, a System.class file and an Object.class file( as every class in java is a child class of Object class). So we will need mostly three to four classes to run this program. 
We can easily run this program with the default JRE, but the JRE provided by Oracle corporation contains 4300+ predefined .class files. When we run this program and we are using the default JRE then we need to maintain all the 4300+ classes on our machine and hence memory is wasted(size of JRE9 is 204mb). To run a 4kb program what is the use of maintaining a 204mb sized JRE. So, on the Clients machine there is a lot of memory wasted. So, a customized JRE is best which will only contain these four classes on the clients machine. And a customized JRE is best when you are using a specific number of classes in the program.So, the biggest use or advantage of customized JRE is that we can create a small JRE by using only the needed modules. Hence, Jlink’s main intention is to avoid shipping everything and also to run on very small devices with little memory. JLink also has a list of plugins that will help optimize our solutions.Syntax:  
jlink [options] –module-path modulepath –add-modules module [, module...]
 
 
Example of JLink using a module based application: 

module demoModule
{
    // keeping it empty
}

Practice.java

package packTest public class Practice {
    public static void main(String[] args)
    {
        System.out.println("JLINK DEMO");
    }
}

To compile a module based application:  
javac --module-source-path src -d com -m -demoModule
Now to run this program we need only two modules one is the demoModule and the other is java.base module ( contains String class, Object class, and System class). Using this two modules we can create our own customized JRE. So add java.base module in src directory. Now we can create our own JRE by using the following JLink command:  
jlink --module-path src --add-modules demoModule, java.base --output demojre
 
Now our own jre is created. Now go to your created jre bin folder like demmojre/bin Now to run:- java -m demoModule/packTest.PracticeOutput:  
JLINK DEMO
To know the size of our own customized jre, we can check it properties.There are a number of plugins available for JLink. Some of them are: –  
exclude-files
class-for-name
include-locales
vm
system-modules
and many more...........
 
simmytarika5
Articles
GBlog
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Docker - COPY Instruction
Time complexities of different data structures
SQL | DROP, TRUNCATE
Difference Between Object And Class
Implementation of LinkedList in Javascript
DSA Sheet by Love Babbar
Must Do Coding Questions for Companies like Amazon, Microsoft, Adobe, ...
GEEK-O-LYMPICS 2022 - May The Geeks Force Be With You!
Geek Streak - 24 Days POTD Challenge
Practice for cracking any coding interview