Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Chapter 2. Creating a custom Java runtime environment for non-modular applications
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
Logger
class.Check the base Red Hat build of OpenJDK 21 binary exists in the
jdk-17
folder:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a directory for your application:
mkdir -p hello-example/sample
$ mkdir -p hello-example/sample
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create
hello-example/sample/HelloWorld.java
file 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 World
application 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
jdeps
command:./jdk-17/bin/jdeps -s ./sample/HelloWorld.class
$ ./jdk-17/bin/jdeps -s ./sample/HelloWorld.class HelloWorld.class -> java.base HelloWorld.class -> java.logging
Copy 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.