DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Improving Java Application Reliability with Dynatrace AI Engine
  • A Systematic Approach for Java Software Upgrades
  • Building a Simple RAG Application With Java and Quarkus
  • Dust Actors and Large Language Models: An Application

Trending

  • Stop Running Two Data Systems for One Agent Query
  • Run Gemma 4 on Your Laptop: A Hands-On Guide to Google's Latest Open Multimodal LLM
  • Dear Micromanager: Your Distrust Has a Job; It’s Just Not the One You’re Doing
  • Why Your DLP Policies Fall Short the Moment AI Agents Enter the Picture
  1. DZone
  2. Coding
  3. Java
  4. Jlink in Java 9

Jlink in Java 9

If you haven't played around with Jlink yet, see what Java 9's new command line tool can do to help you create a lightweight, custom JRE.

By 
Shubham Agarwal user avatar
Shubham Agarwal
·
Jan. 09, 18 · Tutorial
Likes (59)
Comment
Save
Tweet
Share
51.8K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Java (programming language) JRE application

Published at DZone with permission of Shubham Agarwal. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Improving Java Application Reliability with Dynatrace AI Engine
  • A Systematic Approach for Java Software Upgrades
  • Building a Simple RAG Application With Java and Quarkus
  • Dust Actors and Large Language Models: An Application

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook