Using jlink to customize Java runtime environment
Abstract
Providing feedback on Red Hat build of OpenJDK documentation Copy linkLink copied to clipboard!
To report an error or to improve our documentation, log in to your Red Hat Jira account and submit an issue. If you do not have a Red Hat Jira account, then you will be prompted to create an account.
Procedure
- Click the following link to create a ticket.
- Enter a brief description of the issue in the Summary.
- Provide a detailed description of the issue or enhancement in the Description. Include a URL to where the issue occurs in the documentation.
- Clicking Submit creates and routes the issue to the appropriate documentation team.
Making open source more inclusive Copy linkLink copied to clipboard!
Red Hat is committed to replacing problematic language in our code, documentation, and web properties. We are beginning with these four terms: master, slave, blacklist, and whitelist. Because of the enormity of this endeavor, these changes will be implemented gradually over several upcoming releases. For more details, see our CTO Chris Wright’s message.
Chapter 1. Overview of jlink Copy linkLink copied to clipboard!
Jlink is a Java command line tool that is used to generate a custom Java runtime environment (JRE). You can use your customized JRE to run Java applications.
Using jlink, you can create a custom runtime environment that only includes the relevant class file.
Chapter 2. Creating a custom Java runtime environment for non-modular applications Copy linkLink copied to clipboard!
You can create a custom Java runtime environment from a non-modular application by using the jlink tool.
Prerequisites
Install Installing Red Hat build of OpenJDK on RHEL using an archive.
NoteFor best results, use portable Red Hat binaries as a basis for a Jlink runtime, because these binaries contain bundled libraries.
Procedure
Create a simple Hello World application by using the
Loggerclass.Check the base Red Hat build of OpenJDK 21 binary exists in the
jdk-17folder:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a directory for your application:
mkdir -p hello-example/sample
$ mkdir -p hello-example/sampleCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create
hello-example/sample/HelloWorld.javafile with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compile your application:
./jdk-17/bin/javac -d . $(find hello-example -name \*.java)
$ ./jdk-17/bin/javac -d . $(find hello-example -name \*.java)Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run your application without a custom JRE:
./jdk-17/bin/java sample.HelloWorld
$ ./jdk-17/bin/java sample.HelloWorld Mar 09, 2021 10:48:59 AM sample.HelloWorld main INFO: Hello World!Copy to Clipboard Copied! Toggle word wrap Toggle overflow The previous example shows the base Red Hat build of OpenJDK requiring 311 MB to run a single class.
(Optional) You can inspect the Red Hat build of OpenJDK and see many non-required modules for your application:
du -sh jdk-17/
$ du -sh jdk-17/ 313M jdk-17/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Copy to Clipboard Copied! Toggle word wrap Toggle overflow This sample
Hello Worldapplication has very few dependencies. You can use jlink to create custom runtime images for your application. With these images you can run your application with only the required Red Hat build of OpenJDK dependencies.
Determine module dependencies of your application using
jdepscommand:./jdk-17/bin/jdeps -s ./sample/HelloWorld.class
$ ./jdk-17/bin/jdeps -s ./sample/HelloWorld.class HelloWorld.class -> java.base HelloWorld.class -> java.loggingCopy to Clipboard Copied! Toggle word wrap Toggle overflow Build a custom java runtime image for your application:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteRed Hat build of OpenJDK reduces the size of your custom Java runtime image from a 313 M runtime image to a 50 M runtime image.
You can verify the reduced runtime of your application:
./custom-runtime/bin/java sample.HelloWorld
$ ./custom-runtime/bin/java sample.HelloWorld Jan 14, 2021 12:13:26 PM HelloWorld main INFO: Hello World!Copy to Clipboard Copied! Toggle word wrap Toggle overflow The generated JRE with your sample application does not have any other dependencies.
You can distribute your application together with your custom runtime for deployment.
NoteYou must rebuild the custom Java runtime images for your application with every security update of your base Red Hat build of OpenJDK.
Chapter 3. Creating a custom Java runtime environment for modular applications Copy linkLink copied to clipboard!
You can create a custom Java runtime environment from a modular application by using the jlink tool.
Prerequisites
Install Installing Red Hat build of OpenJDK on RHEL using an archive.
NoteFor best results, use portable Red Hat binaries as a basis for a Jlink runtime, because these binaries contain bundled libraries.
Procedure
Create a simple Hello World application by using the
Loggerclass.Check the base Red Hat build of OpenJDK 21 binary exists in the
jdk-17folder:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a directory for your application:
mkdir -p hello-example/sample
$ mkdir -p hello-example/sampleCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create
hello-example/sample/HelloWorld.javafile with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a file called
hello-example/module-info.javaand include the following code in the file:module sample { requires java.logging; }module sample { requires java.logging; }Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compile your application:
./jdk-17/bin/javac -d example $(find hello-example -name \*.java)
$ ./jdk-17/bin/javac -d example $(find hello-example -name \*.java)Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run your application without a custom JRE:
./jdk-17/bin/java -cp example sample.HelloWorld
$ ./jdk-17/bin/java -cp example sample.HelloWorld Mar 09, 2021 10:48:59 AM sample.HelloWorld main INFO: Hello World!Copy to Clipboard Copied! Toggle word wrap Toggle overflow The previous example shows the base Red Hat build of OpenJDK requiring 311 MB to run a single class.
(Optional) You can inspect the Red Hat build of OpenJDK and see many non-required modules for your application:
du -sh jdk-17/
$ du -sh jdk-17/ 313M jdk-17/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Copy to Clipboard Copied! Toggle word wrap Toggle overflow This sample
Hello Worldapplication has very few dependencies. You can use jlink to create custom runtime images for your application. With these images you can run your application with only the required Red Hat build of OpenJDK dependencies.
Create your application module:
mkdir sample-module ./jdk-17/bin/jmod create --class-path example/ --main-class sample.HelloWorld --module-version 1.0.0 -p example sample-module/hello.jmod
$ mkdir sample-module $ ./jdk-17/bin/jmod create --class-path example/ --main-class sample.HelloWorld --module-version 1.0.0 -p example sample-module/hello.jmodCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a custom JRE with the required modules and a custom application launcher for your application:
./jdk-17/bin/jlink --launcher hello=sample/sample.HelloWorld --module-path sample-module --add-modules sample --output custom-runtime
$ ./jdk-17/bin/jlink --launcher hello=sample/sample.HelloWorld --module-path sample-module --add-modules sample --output custom-runtimeCopy to Clipboard Copied! Toggle word wrap Toggle overflow List the modules of the produced custom JRE.
Observe that only a fraction of the original Red Hat build of OpenJDK remains.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteRed Hat build of OpenJDK reduces the size of your custom Java runtime image from a 313 M runtime image to a 50 M runtime image.
Launch the application using the
hellolauncher:./custom-runtime/bin/hello
$ ./custom-runtime/bin/hello Jan 14, 2021 12:13:26 PM HelloWorld main INFO: Hello World!Copy to Clipboard Copied! Toggle word wrap Toggle overflow The generated JRE with your sample application does not have any other dependencies besides
java.base,java.logging, andsamplemodule.You can distribute your application that is bundled with the custom runtime in
custom-runtime. This custom runtime includes your application.NoteYou must rebuild the custom Java runtime images for your application with every security update of your base Red Hat build of OpenJDK.
Revised on 2024-05-09 14:53:28 UTC