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
11 Monitoring and Observability Tools for 2023
Learn more

JPMS and JLink Example

Learn more about implementing JLink and JPMS in Java 9.

Mostafa Rastgar user avatar by
Mostafa Rastgar
·
May. 16, 19 · Tutorial
Like (2)
Save
Tweet
Share
6.46K Views

Join the DZone community and get the full member experience.

Join For Free

In Java 9, JLink is a new Java command tool through which we can create our customized JRE with the most minimal dependencies. In this article, I will describe how to achieve this goal.

What's the Problem

Suppose that you are developing an application that only depends on classes in the 'java.lang' package. In legacy JRE versions, you had to install the whole JRE in target machine to run your application. But it has some drawbacks, such as:

  • Waste of memory and a negative impact on performance
  • Will not be able to develop microservices that contain very little memory
  • Not suitable for IoT devices

As a result of the above reasons, it is highly recommended to customize and tune your specific JRE. An amazing tool is provided in Java 9 called JLink. I will describe how to use it in the coming sections.

Sample Project Description

A sample project called TestModuls is attached here. It contains two modules named Test and Utils. The package model of the Utils module is:

Image title

We are supposed to expose our GlobalUtils as a third party for other modules, but LocalUtils is just for internal usage. So, the module-info.java in the Utils module looks like this:

module Utils{
    exports com.mostafa.utils.global;
}


The test module has a dependency on the Utils module, and it only has a main class called Test to test GlobalUtils methods placed in the Utils module. So, the module-info.java in the test module is like this:

module test{
    requires Utils;
}


We can easily build the two modules separately using the Maven pom available in each module. Now, we are ready to build our custom JRE.

Use JLink

It, obviously, is clear that the TestModuls project just depends on classes in 'java.lang' package. So, it is necessary to install an JRE-based module in the target machine. The JRE modules are available in  ${JAVA_HOME}/jmods and our considered module isjava.base.jmod.

For this approach, it is needed to consider the required modules while we are customizing the JRE. To do that, it is better to go to the root of the TestModuls directory. Then, we run the following command:

jlink --module-path Test/target/classes;Utils/target/classes;"${JAVA_HOME}/jmods" --add-modules Utils,test,java.base --output myjre


Where:

  • --module-path implies the physical addresses of various modules separated by ';'
  • --add-modules implies the module names which is required for this custom JRE seperated by ','
  • --output implies the physical address which you would like to create your custom JRE

Afterward, we can see that a directory called myjre will be created in the TestModuls directory, which will be our custom JRE.

How to Run the Application With a Specific JRE

Go to myjre/bin directory; then run the following command:

java -m test/com.mostafa.test.Test


First, we should determine which modules our main class is placed. Then, after '/,' we should mention the fully qualified main class name. Then, we can see that the output message will be displayed, which is 'dhis message text is roundeT.'

Conclusion

In this article, we learned how to make our project modular using modular feature available from Java 9. We can see that in the test module we cannot use the LocalUtils class methods and it provides us with the encapsulation feature. Moreover, we just build our custom JRE, which its size is less than 40 MB. We see that it has much lower volume than the default JRE, which is about 300 MB.

I hope you found this article helpful!

JRE

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Build an Automated Testing Pipeline With GitLab CI/CD and Selenium Grid
  • HTTP vs Messaging for Microservices Communications
  • How To Best Use Java Records as DTOs in Spring Boot 3
  • Microservices 101: Transactional Outbox and Inbox

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
  • +1 (919) 678-0300

Let's be friends: