diff --git a/core/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/core/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 45b8b545ad8..b0e1bcf6493 100644 --- a/core/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/core/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -420,7 +420,7 @@ public class SpringApplication { private void addAotGeneratedInitializerIfNecessary(List> initializers) { if (NativeDetector.inNativeImage()) { - checkNativeImageVersion(); + NativeImageRequirementsException.throwIfNotMet(); } if (AotDetector.useGeneratedArtifacts()) { List> aotInitializers = new ArrayList<>( @@ -438,15 +438,6 @@ public class SpringApplication { } } - private void checkNativeImageVersion() { - JavaVersion minRequiredJavaVersion = JavaVersion.TWENTY_FIVE; - if (JavaVersion.getJavaVersion().isOlderThan(minRequiredJavaVersion)) { - throw new NativeImageRequirementsNotMetException( - "Native Image requirements not met, please upgrade it. Native Image must support at least Java %s" - .formatted(minRequiredJavaVersion)); - } - } - private void refreshContext(ConfigurableApplicationContext context) { if (this.properties.isRegisterShutdownHook()) { shutdownHook.registerApplicationContext(context); @@ -1661,16 +1652,24 @@ public class SpringApplication { /** * Exception which is thrown if GraalVM's native-image requirements aren't met. */ - static final class NativeImageRequirementsNotMetException extends RuntimeException { + static final class NativeImageRequirementsException extends RuntimeException { - /** - * Creates a new {@link NativeImageRequirementsNotMetException} instance. - * @param message the message - */ - NativeImageRequirementsNotMetException(String message) { + private static final JavaVersion MINIMUM_REQUIRED_JAVA_VERSION = JavaVersion.TWENTY_FIVE; + + private static final JavaVersion CURRENT_JAVA_VERSION = JavaVersion.getJavaVersion(); + + NativeImageRequirementsException(String message) { super(message); } + static void throwIfNotMet() { + if (CURRENT_JAVA_VERSION.isOlderThan(MINIMUM_REQUIRED_JAVA_VERSION)) { + throw new NativeImageRequirementsException("Native Image requirements not met. " + + "Native Image must support at least Java %s but Java %s was detected" + .formatted(MINIMUM_REQUIRED_JAVA_VERSION, CURRENT_JAVA_VERSION)); + } + } + } /** diff --git a/core/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/core/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 6be06448c51..08216f19677 100644 --- a/core/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/core/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -59,7 +59,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.DefaultBeanNameGenerator; import org.springframework.boot.Banner.Mode; -import org.springframework.boot.SpringApplication.NativeImageRequirementsNotMetException; +import org.springframework.boot.SpringApplication.NativeImageRequirementsException; import org.springframework.boot.availability.AvailabilityChangeEvent; import org.springframework.boot.availability.AvailabilityState; import org.springframework.boot.availability.LivenessState; @@ -77,6 +77,7 @@ import org.springframework.boot.context.event.ApplicationStartingEvent; import org.springframework.boot.context.event.SpringApplicationEvent; import org.springframework.boot.convert.ApplicationConversionService; import org.springframework.boot.env.DefaultPropertiesPropertySource; +import org.springframework.boot.system.JavaVersion; import org.springframework.boot.testsupport.classpath.ForkedClassPath; import org.springframework.boot.testsupport.classpath.resources.WithResource; import org.springframework.boot.testsupport.system.CapturedOutput; @@ -753,9 +754,10 @@ class SpringApplicationTests { try { SpringApplication application = new SpringApplication(); application.setWebApplicationType(WebApplicationType.NONE); - assertThatExceptionOfType(NativeImageRequirementsNotMetException.class).isThrownBy(application::run) - .withMessage( - "Native Image requirements not met, please upgrade it. Native Image must support at least Java 25"); + assertThatExceptionOfType(NativeImageRequirementsException.class).isThrownBy(application::run) + .withMessage("Native Image requirements not met. " + + "Native Image must support at least Java 25 but Java %s was detected" + .formatted(JavaVersion.getJavaVersion())); } finally { System.clearProperty("org.graalvm.nativeimage.imagecode");