Questo contenuto non è disponibile nella lingua selezionata.
Chapter 2. Creating a custom Java runtime environment for non-modular applications
You can create a custom Java runtime environment (JRE) from a non-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 Compile your application:
./jdk-11/bin/javac -d . $(find hello-example -name \*.java)
$ ./jdk-11/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-11/bin/java sample.HelloWorld
$ ./jdk-11/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 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.
Determine module dependencies of your application using
jdepscommand:./jdk-11/bin/jdeps -s ./sample/HelloWorld.class
$ ./jdk-11/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 NoteThe size of your custom java runtime image is being reduced to 50M runtime image from 313M 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.
Rebuild the custom java runtime images for your application with every security update of your base Red Hat build of OpenJDK.