Generate custom JRE from JDK using jlink
For around 20 years the JDK shipped with a JRE, which was just a subset of it. Finally, with Java 11, Oracle and the OpenJDK team decided to end this and just distribute a single thing, the JDK. But there are many reasons you still want to deploy JRE on your server, these reasons may be security related or your application simply doesn’t require all the modules and including them is a waste of memory. So, let’s see how you can create your custom JRE with custom modules extracted from JDK using jlink.
jlink is a tool (included with jdk) that generates a custom Java runtime image that contains only the platform modules that are required for a given application.
With jlink, we can create our own, small JRE that contains only the relevant classes that we want to use, without wasting memory, and as a result, we’ll see increased performance.
Create custom JRE images with 3 simple steps

Create custom JRE images with 3 simple steps
Step 1. Compile any extra module, apart from modules included in JDK, which you may want to include in your JRE using javac.
Step 2. Use jdeps to List the Dependent Modules
jdeps --module-path out -s --module exampleModule
Example output can be:
jlinkModule -> java.base
jlinkModule -> java.logging
This means our example module is dependent on java.base and java.logging. you can also generate the list of available Java modules using the following command —
java --list-modules
Step 3. To create a custom JRE, we can use the jlink command. Here’s its basic syntax:
jlink --output <output folder name> --compress=2 --no-header-files --no-man-pages --module-path ..\jmods --add-modules java.base,java.logging
Let’s see explanation of the parameters used
--output <path>
— Specifies the location of the generated JRE
--compress=<0|1|2>
— Compresses all resourced in the output image
- Level 0: No compression
- Level 1: Constant string sharing
- Level 2: ZIP
--no-header-files
— Excludes header files
--no-man-pages
— Excludes man pages
--module-path <modulepath>
— Specifies module path
--add-modules <module 1>,<module 2>,...<module n>
— Adds the named module to the default set of root modules.
Your JRE will be created in the mentioned directory.
For more information about the java modules please refer: https://docs.oracle.com/en/java/javase/11/docs/api/
Source of information:
— hritikchaudhary