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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Integrating AWS With Salesforce Using Terraform
  • The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables
  • Top Six React Development Tools
  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL

Trending

  • Integrating AWS With Salesforce Using Terraform
  • The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables
  • Top Six React Development Tools
  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
  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.

Shubham Agarwal user avatar by
Shubham Agarwal
·
Jan. 09, 18 · Tutorial
Like (59)
Save
Tweet
Share
50.44K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Integrating AWS With Salesforce Using Terraform
  • The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables
  • Top Six React Development Tools
  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: