Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Chapter 3. Creating a custom Java runtime environment for modular applications
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
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 Create a file called
hello-example/module-info.java
and 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 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.
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.jmod
Copy 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-runtime
Copy 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
hello
launcher:./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
, andsample
module.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