Open In App

JLink | Java Linker

Last Updated : 01 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Now, what is the need of creating our own customized JRE?

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…]

Structure of a Module based application:

Example of JLink using a module based application:

module-info.java




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.Practice

Output:

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
  • claas-for-name
  • include-locales
  • vm
  • system-modules
  • and many more………..


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads