[JAVA] How to make a lightweight JRE for distribution


Starting with Java 9, modularization with Project Jigsaw makes it easy to create lightweight JREs for distribution. Lightweight JREs contain only the bare essentials that your application uses, so you can keep your file size very small. Therefore, it is an essential support when bundling JRE with an application.

This article describes how to make a lightweight JRE.


1. Obtaining OpenJDK

First, get OpenJDK.

This time, I used the latest stabilizer OpenJDK 11 as of 02/08/2019. I will.

When the ZIP file is expanded, the folder structure is as follows.

 ├─ bin
 ├─ conf
 ├─ include
 ├─ jmods
 ├─ legal
 ├─ lib
 └─ release

Use the bin and jmods folders to generate a lightweight JRE.

2. Dependency check

Check the dependencies of the packages used by the application (jar file) with the jdeps command contained in the jdk-11 / bin folder. (You can also specify it with a wildcard like * .jar)

cd jdk-11\bin
jdeps.exe --list-deps --ignore-missing-deps C:\example\app.jar

When you run the command, you will see a list of packages used by that application. You can see that the ʻapp.jar` example above depends on the following packages:


In other words, if at least these packages are included in the JRE to run the application, it will be a Java runtime environment.

3. Generate a JRE that contains only specific packages

Now that we have a list of packages used by our application with the jdeps command, we will generate a JRE that contains only these packages.

To generate a JRE, just pass the required packages to the jlink command. When passing multiple packages, separate them with commas.

jlink.exe --compress=2 --module-path ..\jmods --add-modules java.base,java.desktop,java.logging,java.sql,java.xml --output jre

When you run the command, a jre folder will be created in the current folder. This generated folder is a lightweight JRE.

All you have to do now is use this lightweight JRE as your application's Java runtime environment. It can be used without any problem as long as it does not depend on the new package due to modification on the application side.

File size comparison

The following results were obtained when comparing the file sizes of the original version of OpenJDK 11 and the lightweight version of the JRE containing only a specific package [^ 1].

Original version Lightweight version
299M 46M

The lightweight version of JRE contains only the minimum required packages for your application, so the file size is kept small.

Due to the large file size differences, it is recommended that you generate a lightweight JRE before bundling it with your application.

Recommended Posts

How to make a lightweight JRE for distribution
How to make a mod for Slay the Spire
How to make a Java container
How to make a JDBC driver
How to make a splash screen
How to make a Jenkins plugin
How to make a Maven project
How to make a Java array
How to make a Java calendar Summary
How to create a Maven repository for 2020
How to make a Discord bot (Java)
How to make a groundbreaking diamond using Java for statement wwww
How to create a database for H2 Database anywhere
How to create pagination for a "kaminari" array
How to make shaded-jar
How to make a follow function in Rails
[Java] How to make multiple for loops single
How to make a judgment method to search for an arbitrary character in an array
How to make a factory with a model with polymorphic association
[Rails] How to create a signed URL for CloudFront
How to use an array for a TreeMap key
How to write a unit test for Spring Boot 2
[Spring Boot] How to create a project (for beginners)
How to make Laravel faster with Docker for Mac
How to make a cache without thinking too much
Java --How to make JTable
How to leave a comment
[Rails] How to make seed
How to insert a video
How to create a method
SDWebImage: How to clear the cache for a particular UIImageView
Learning Ruby with AtCoder 13 How to make a two-dimensional array
How to test a private method with RSpec for yourself
How to output array values without using a for statement
Minecraft Modding [1.12] How to attach a special render for Item
How to create and launch a Dockerfile for Payara Micro
How to add columns to a table
Try to make a simple callback
How to specify validation for time_field
How to install JMeter for Mac
How to sign a Minecraft MOD
[Java] How to create a folder
How to write a ternary operator
[Swift] How to send a notification
Try to make a peepable iterator
[Android] How to make Dialog Fragment
How to make a jar file with no dependencies in Maven
How to identify the path that is easy to make a mistake
How to check for the contents of a java fixed-length string
[Xcode] How to add a README.md file
How to execute a contract using web3j
How to sort a List using Comparator
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
How to use binding.pry for view files
How to install Play Framework 2.6 for Mac
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Basic] How to write a Dockerfile Self-learning ②
How to insert a video in Rails
[Java] How to turn a two-dimensional array with an extended for statement
How to add a new hash / array