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.
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.
jlink [options] –module-path modulepath –add-modules module [, module…]
Structure of a Module based application:
Example of JLink using a module based application:
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
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: –
- and many more………..
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.Collections.disjoint() Method in java with Examples
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java lang.Long.reverse() method in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.math.BigInteger.modInverse() method in Java
- Java.util.LinkedList.get(), getFirst(), getLast() in Java
- Java.util.TreeMap.pollFirstEntry() and pollLastEntry() in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.