Questo contenuto non è disponibile nella lingua selezionata.
Chapter 3. Creating a custom Java runtime environment for modular applications
You can create a custom Java runtime environment from a modular application using the jlink tool.
Prerequisites
Install Red Hat build of OpenJDK 11.
NoteIt is always recommended to use the portable tarball as a basis of Jlink’ed runtime.
Procedure
Create a simple Hello World application using Logger class.
You have the base Red Hat build of OpenJDK 11 available in the
jdk-11folder: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
hello-example/module-info.javafile with the following content:module sample { requires java.logging; }module sample { requires java.logging; }Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compile your application:
./jdk-11/bin/javac -d example $(find hello-example -name \*.java)
$ ./jdk-11/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-11/bin/java -cp example sample.HelloWorld
$ ./jdk-11/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 In this case, the base Red Hat build of OpenJDK takes 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-11/
$ du -sh jdk-11/ 313M jdk-11/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Copy to Clipboard Copied! Toggle word wrap Toggle overflow Sample
Hello Worldapplication has very few dependencies. You can use jlink to create custom runtime images for your application. These images help you run your application with only required Red Hat build of OpenJDK dependencies.
Create your application module:
mkdir sample-module ./jdk-11/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-11/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-11/bin/jlink --launcher hello=sample/sample.HelloWorld --module-path sample-module --add-modules sample --output custom-runtime
$ ./jdk-11/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 NoteThe size of your custom java runtime image is being reduced to 50M runtime image from 313M 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 other than
java.base,java.logging, andsamplemodule.You can distribute your application bundled with the custom runtime in
custom-runtime. It includes your application.
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 16:48:50 UTC