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

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
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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Using Lombok Library With JDK 23
  • A Maven Story
  • Step By Step Guide To Using Mule ESB
  • Using Python Libraries in Java

Trending

  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • How To Develop a Truly Performant Mobile Application in 2025: A Case for Android
  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  1. DZone
  2. Coding
  3. Java
  4. JDK 9: Creating a Java Runtime Image With Maven

JDK 9: Creating a Java Runtime Image With Maven

Java runtime images are designed for projects to run in a target environment without the need for a JRE installation. Here's how Java 9 and Maven will make them work.

By 
Karl Heinz Marbaise user avatar
Karl Heinz Marbaise
·
Jun. 29, 17 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
19.0K Views

Join the DZone community and get the full member experience.

Join For Free

At the moment, JDK 9 is only available as Early Access (EA) to let the community take a look how it works and what can be improved. Apart from all the news, for example about the modular system Jigsaw, there is one important question:

How can I create a Java runtime with Maven? But before we can begin, let's do a quick recap of runtime images.

Say you have a running Java runtime environment where all the parts that aren't really used by the current application have been removed. Based on the modularization of the Java platform itself, you can think of many parts that can be removed by a usual application. This, in the end, will give you the opportunity to deliver only the Java runtime image including your application. So this means in the target environment, you do not need to install a full blown JRE. This saves space in particular if you think about microservices.

There are a few things that need to be paid attention to. Everything written here is:

  • Currently in a proof of concept state.

  • Speculative.

So this means it might become an official Maven plugin. The intention is to get some feedback from interested volunteers — to see others working with JDK 9 and to learn about their experiences and to see if Maven needs to go in a different direction.

So let's start with the interesting part. Let's start with a simple multi-module project and see how this works. The example project currently contains three modules, mod-1, mod-2 , and mod-jar. The mod-jar is a usual JAR project that produces a JAR file as result.

The only important part is that this contains a module-info.java file, which is compiled into the JAR file. This JAR file is a modular JAR file because it contains a module-info.class file at the root location of the JAR.

Meanwhile, mod-1 and mod-2 are currently explicitly producing jmod files, which contain the module name in its file name. At the moment, those modules are placed into the target/jmods/ directory of the appropriate project.

So now we have summarized the different projects that produce either a JAR or jmod file, but to get a Java runtime image, we have to use the jlink command of JDK 9, which is the Java Linker that produces the runtime image. Then handling of jlink is currently put into the mod-jlink module, which contains a packaging of jlink.

The example project uses two newly created Maven plugins (maven-jmod-plugin and maven-jlink-plugin) that currently implement the handling for jmod and the jlink packaging type. They exist only as SNAPSHOTs that are available via the Apache Software Foundation repository. If you would like to use them, you have to configure them into your settings.xml or in your repository manager. One more thing is needed to be set up: toolchains to configure the environment, like where to find the JDK 9, etc.

If you run the whole project via:

mvn clean package
...
[INFO] ------------------------------------------------------------------------
[INFO] Building com.soebes.nine.one 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ com.soebes.nine.one ---
[INFO] Deleting /Users/kama/jdk-9-test/maven-example/mod-1/target
[INFO]
[INFO] --- maven-toolchains-plugin:1.1:toolchain (default) @ com.soebes.nine.one ---
[INFO] Required toolchain: jdk [ vendor='oracle' version='1.9' ]
[INFO] Found matching toolchain for type jdk: JDK[/Library/Java/JavaVirtualMachines/jdk1.9.0_ea+172.jdk/Contents/Home]
[INFO]....
...
[INFO]
[INFO] --- maven-compiler-plugin:3.6.0:compile (default-compile) @ com.soebes.nine.one ---
[INFO] Toolchain in maven-compiler-plugin: JDK[/Library/Java/JavaVirtualMachines/jdk1.9.0_ea+172.jdk/Contents/Home]
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /Users/kama/jdk-9-test/maven-example/mod-1/target/classes
[INFO]
...
...
[INFO]
[INFO] --- maven-jlink-plugin:3.0.0-SNAPSHOT:jlink (default-jlink) @ mod-jlink ---
[INFO] Toolchain in maven-jlink-plugin: jlink [ /Library/Java/JavaVirtualMachines/jdk1.9.0_ea+172.jdk/Contents/Home/bin/jlink ]
[INFO] The following dependencies will be linked into the runtime image:
[INFO]  -> com.soebes.examples.jdk9:com.soebes.nine.one:jmod:1.0-SNAPSHOT
[INFO]  -> com.soebes.examples.jdk9:com.soebes.nine.two:jmod:1.0-SNAPSHOT
[INFO]  -> com.soebes.examples.jdk9:com.soebes.nine.jar:jar:1.0-SNAPSHOT
[INFO] Building zip: /Users/kama/jdk-9-test/maven-example/mod-jlink/target/mod-jlink-1.0-SNAPSHOT.zip
[INFO]
[INFO] --- maven-site-plugin:3.6:attach-descriptor (attach-descriptor) @ mod-jlink ---
[INFO] Skipping because packaging 'jlink' is not pom.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] parent ............................................. SUCCESS [  0.931 s]
[INFO] com.soebes.nine.one ................................ SUCCESS [ 17.019 s]
[INFO] com.soebes.nine.two ................................ SUCCESS [  1.007 s]
[INFO] com.soebes.nine.jar ................................ SUCCESS [  0.926 s]
[INFO] mod-jlink .......................................... SUCCESS [  2.602 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 23.200 s
[INFO] Finished at: 2017-06-06T20:08:54+02:00
[INFO] Final Memory: 19M/385M
[INFO] ------------------------------------------------------------------------


If everything works, then you will find the Java runtime image:

cd mod-jlink/target/jlink
~/jdk-9-test/maven-example/mod-jlink/target/jlink (master)$ ls -la
total 8
drwxr-xr-x   8 kama  staff  272 Jun  6 00:10 .
drwxr-xr-x   4 kama  staff  136 Jun  6 00:10 ..
drwxr-xr-x   4 kama  staff  136 Jun  6 00:10 bin
drwxr-xr-x   4 kama  staff  136 Jun  6 00:10 conf
drwxr-xr-x   7 kama  staff  238 Jun  6 00:10 include
drwxr-xr-x   3 kama  staff  102 Jun  6 00:10 legal
drwxr-xr-x  19 kama  staff  646 Jun  6 00:10 lib
-rw-r--r--   1 kama  staff   97 Jun  6 00:10 release


You can check the Java runtime first via:

~/jdk-9-test/maven-example/mod-jlink/target/jlink (master)$ bin/java --list-modules
com.soebes.nine.jar
com.soebes.nine.one@1.0-SNAPSHOT
com.soebes.nine.two@1.0-SNAPSHOT
java.base@9-ea


which shows that this JRE only contains those modules which are needed. Of course, you can check the size of generated image on the hard drive:

~/jdk-9-test/maven-example/mod-jlink/target/jlink (master)$ du -h -d 1 .
144K    ./bin
 76K    ./conf
184K    ./include
 44K    ./legal
 35M    ./lib
 35M    .


So this means the whole Java runtime image is only 35 MiB in size. You can check against a full-fledged JRE and compare that with the 35 MiB.

To test different modules in that Java runtime image, you can call them like this:

~/jdk-9-test/maven-example/mod-jlink/target/jlink (master)$ bin/java com.soebes.example.nine.jar.Main
Main from Java 9 (jar)
~/jdk-9-test/maven-example/mod-jlink/target/jlink (master)$ bin/java com.soebes.example.nine.one.Main
Main one from Java 9
~/jdk-9-test/maven-example/mod-jlink/target/jlink (master)$ bin/java com.soebes.example.nine.two.Main
Main two from Java 9

Conclusion

This is the first step of Maven to support of JDK 9 tools like jlink and jmod. Other tools, like maven-jdeps-plugin, have existed for a longer time.

This is currently a proof of concept and not an official statement. This is intended to get feedback from others working with JDK 9 and see if we are on the right track.

Apart from that, you see the opportunities that JDK 9 with its runtime images is offering. Based on the current implementation of the plugins, it would not be very complicated to migrate an existing application (only command line apps??) to a Java runtime image.

Java (programming language) Java Development Kit Apache Maven

Published at DZone with permission of Karl Heinz Marbaise. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Using Lombok Library With JDK 23
  • A Maven Story
  • Step By Step Guide To Using Mule ESB
  • Using Python Libraries in Java

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: