diff --git a/core/runtime/src/main/java/io/quarkus/runtime/util/JavaVersionUtil.java b/core/runtime/src/main/java/io/quarkus/runtime/util/JavaVersionUtil.java index 6edb9398e2a08..ab1f5d53c3ea4 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/util/JavaVersionUtil.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/util/JavaVersionUtil.java @@ -12,6 +12,7 @@ public class JavaVersionUtil { private static boolean IS_GRAALVM_JDK; private static boolean IS_JAVA_16_OR_OLDER; private static boolean IS_JAVA_17_OR_NEWER; + private static boolean IS_JAVA_19_OR_NEWER; static { performChecks(); @@ -26,11 +27,13 @@ static void performChecks() { IS_JAVA_13_OR_NEWER = (first >= 13); IS_JAVA_16_OR_OLDER = (first <= 16); IS_JAVA_17_OR_NEWER = (first >= 17); + IS_JAVA_19_OR_NEWER = (first >= 19); } else { IS_JAVA_11_OR_NEWER = false; IS_JAVA_13_OR_NEWER = false; IS_JAVA_16_OR_OLDER = false; IS_JAVA_17_OR_NEWER = false; + IS_JAVA_19_OR_NEWER = false; } String vmVendor = System.getProperty("java.vm.vendor"); @@ -53,6 +56,10 @@ public static boolean isJava17OrHigher() { return IS_JAVA_17_OR_NEWER; } + public static boolean isJava19OrHigher() { + return IS_JAVA_19_OR_NEWER; + } + public static boolean isGraalvmJdk() { return IS_GRAALVM_JDK; } diff --git a/extensions/awt/deployment/src/main/java/io/quarkus/awt/deployment/AwtProcessor.java b/extensions/awt/deployment/src/main/java/io/quarkus/awt/deployment/AwtProcessor.java index 8a7a53e80aece..e9664e1cb25e4 100644 --- a/extensions/awt/deployment/src/main/java/io/quarkus/awt/deployment/AwtProcessor.java +++ b/extensions/awt/deployment/src/main/java/io/quarkus/awt/deployment/AwtProcessor.java @@ -1,7 +1,6 @@ package io.quarkus.awt.deployment; import static io.quarkus.deployment.builditem.nativeimage.UnsupportedOSBuildItem.Os.WINDOWS; -import static io.quarkus.deployment.pkg.steps.GraalVM.Version.CURRENT; import java.util.ArrayList; import java.util.List; @@ -10,6 +9,7 @@ import org.jboss.logging.Logger; +import io.quarkus.awt.runtime.graal.AwtFeature; import io.quarkus.awt.runtime.graal.DarwinAwtFeature; import io.quarkus.deployment.Feature; import io.quarkus.deployment.annotations.BuildProducer; @@ -26,9 +26,7 @@ import io.quarkus.deployment.pkg.builditem.NativeImageRunnerBuildItem; import io.quarkus.deployment.pkg.builditem.ProcessInheritIODisabled; import io.quarkus.deployment.pkg.builditem.ProcessInheritIODisabledBuildItem; -import io.quarkus.deployment.pkg.steps.GraalVM; import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild; -import io.quarkus.deployment.pkg.steps.NoopNativeImageBuildRunner; class AwtProcessor { @@ -42,6 +40,7 @@ FeatureBuildItem feature() { @BuildStep(onlyIf = NativeOrNativeSourcesBuild.class) void nativeImageFeatures(BuildProducer nativeImageFeatures) { nativeImageFeatures.produce(new NativeImageFeatureBuildItem(DarwinAwtFeature.class)); + nativeImageFeatures.produce(new NativeImageFeatureBuildItem(AwtFeature.class)); } @BuildStep(onlyIf = NativeOrNativeSourcesBuild.class) @@ -128,12 +127,6 @@ JniRuntimeAccessBuildItem setupJava2DClasses(NativeImageRunnerBuildItem nativeIm Optional processInheritIODisabledBuildItem) { nativeImageRunnerBuildItem.getBuildRunner() .setup(processInheritIODisabled.isPresent() || processInheritIODisabledBuildItem.isPresent()); - final GraalVM.Version v; - if (nativeImageRunnerBuildItem.getBuildRunner() instanceof NoopNativeImageBuildRunner) { - v = CURRENT; - } else { - v = nativeImageRunnerBuildItem.getBuildRunner().getGraalVMVersion(); - } final List classes = new ArrayList<>(); classes.add("com.sun.imageio.plugins.jpeg.JPEGImageReader"); classes.add("com.sun.imageio.plugins.jpeg.JPEGImageWriter"); @@ -258,18 +251,11 @@ JniRuntimeAccessBuildItem setupJava2DClasses(NativeImageRunnerBuildItem nativeIm // A new way of dynamically loading shared objects instead // of baking in static libs: https://github.com/oracle/graal/issues/4921 classes.add("sun.awt.X11FontManager"); - if (v.javaVersion.feature() != 19) { - classes.add("java.awt.GraphicsEnvironment"); - classes.add("sun.awt.X11GraphicsConfig"); - classes.add("sun.awt.X11GraphicsDevice"); - classes.add("sun.java2d.SunGraphicsEnvironment"); - classes.add("sun.java2d.xr.XRSurfaceData"); - } - - // Added for JDK 19+ due to: https://github.com/openjdk/jdk20/commit/9bc023220 calling FontUtilities - if (v.jdkVersionGreaterOrEqualTo("19")) { - classes.add("sun.font.FontUtilities"); - } + classes.add("java.awt.GraphicsEnvironment"); + classes.add("sun.awt.X11GraphicsConfig"); + classes.add("sun.awt.X11GraphicsDevice"); + classes.add("sun.java2d.SunGraphicsEnvironment"); + classes.add("sun.java2d.xr.XRSurfaceData"); return new JniRuntimeAccessBuildItem(true, true, true, classes.toArray(new String[0])); } diff --git a/extensions/awt/runtime/src/main/java/io/quarkus/awt/runtime/graal/AwtFeature.java b/extensions/awt/runtime/src/main/java/io/quarkus/awt/runtime/graal/AwtFeature.java new file mode 100644 index 0000000000000..1931f50a6632e --- /dev/null +++ b/extensions/awt/runtime/src/main/java/io/quarkus/awt/runtime/graal/AwtFeature.java @@ -0,0 +1,24 @@ +package io.quarkus.awt.runtime.graal; + +import org.graalvm.nativeimage.hosted.Feature; +import org.graalvm.nativeimage.hosted.RuntimeJNIAccess; + +import io.quarkus.runtime.util.JavaVersionUtil; + +public class AwtFeature implements Feature { + @Override + public void afterRegistration(AfterRegistrationAccess access) { + // Added for JDK 19+ due to: https://github.com/openjdk/jdk20/commit/9bc023220 calling FontUtilities + if (JavaVersionUtil.isJava19OrHigher()) { + try { + Class fontUtilitiesClass = Class.forName("sun.font.FontUtilities"); + RuntimeJNIAccess.register(fontUtilitiesClass); + RuntimeJNIAccess.register(fontUtilitiesClass.getDeclaredFields()); + RuntimeJNIAccess.register(fontUtilitiesClass.getDeclaredMethods()); + RuntimeJNIAccess.register(fontUtilitiesClass.getDeclaredConstructors()); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + } +}