JLINK in Java-9


What is JLINK (java linker)   –
JLINK is java’s new command line tool through which we can create our own customised JRE.

usually, we run our program using default JRE which is provided by Oracle or JAVA people but in case if you wants to create your own JRE than you can go with JLINK concept.

Why our own JRE – 

I think you people have a question in your mind that – JAVA people provided the default JRE and we can happily run our program using this default JRE then what is need of creating our own JRE
so let’s take an example through which it will be clear that why we need to create customised JRE and what is the problem with default JRE –
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 my system, I need to install a default JRE which is provided by JAVA people. After installing default JRE I can happily run my small “hello world” application.

But the problem is  – 

If you will see, for executing this small “hello world” application how many .class files we required to have –

  • Test.class
  • String.class
  • System.class
  • Object.class

Here  3 to 4  .class will be enough to run my application.

But the default JRE which is provided by Oracle contains 4300+ predefined java .class files.
If I will execute my “hello world” application with default JRE than all the predefine .class files will be executed but I need only 3 to 4 .class files for executing my “hello world” application then why I need to maintain another .class file.

So The problem with default JRE is  – default JRE executes the all predefined .class file whether you want or not 

And if you will also look into default JRE size then it is 203 MB, and for executing my simple 1 kb of code, I have to maintain 203 sizes of JRE in our machine so it is completely wastage of Memory.

So using default JRE means –

  • Wastage of Memory and performance will get Down 
  • Will not be able to develop a small microservices which contain very less memory and more java application has to run.
  •  Not suitable for IoT devices

so java was not the best choice for microservices and IoT Devices, but this is the problem until java 1.8 version only, to overcome this problem Java 1.9 comes with JLINK. Through the JLINK we can create our own small JRE that contains the only relevant class that we want to have, will not be wastage of memory and performance will increases.
JLINK allow us to link sets of only required modules to create a runtime image(our own JRE) 

Creation of our own JRE only with required modules 

Suppose my “hello world” program is in a Module that is DemoModule and we can compile our module based application in java9 like  –

compilation :
javac –module-source-path src -d out -m demoModule 
After compilation successfully, out folder will be created that has Test.class file.
If you run this module based application using default JRE than you can use command –
java –module-path out -m demoModule/knoldus.Test
But as we discussed our “hello world” program required only a few .class files which are – String.class , System.class and Object.class
These .class files are the part of java.lang package and java.lang package is the part of java.base module, so if I want to run my “hello word” program only two modules are required – DemoModule and java.base Module so with these two modules we can create our own customised JRE and can run this application.
you can find java.base module in the path –

java\jdk-9\jmods

so just copy the java.base module and paste it the out folder that has Test.class file
Now we can create our own JRE by using 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 customised JRE and in this case, if you will look the size of customised JRE then it’s around 32 MB. 
just follow some steps to execute your program by using customised JRE

  • cd myjre
  • cd bin 
  • java -m demoModule/knoldus.Test

by executing these commands you can happily run your “hello world” application.

so this is all about JLINK, I hope now you people have a clear picture of JRE.


knoldus-advt-sticker


 

This entry was posted in Scala. Bookmark the permalink.

4 Responses to JLINK in Java-9

  1. What was the final size of you custom jre?

  2. Pingback: Create your own mini JRE in Java-9 — Knoldus – Randika Prabhashvara

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s