Skip to content

Fix GraalVM native compilation issue with Java 22 #3833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

academey
Copy link

Summary

  • Fixes native compilation failure when using Java 22 with GraalVM
  • Adds proper RuntimeHints for SLF4J service providers
  • Ensures compatibility with both Java 21 and Java 22

Details

This PR resolves issue #494 where native compilation fails under Java 22 but works on Java 21. The root cause was that SLF4J service providers (NOP_FallbackServiceProvider and SubstituteServiceProvider) were being initialized at runtime instead of build time in GraalVM native images.

Changes made:

  1. Updated SpringAiCoreRuntimeHints.java to register SLF4J classes for reflection and build-time initialization
  2. Added comprehensive tests in SpringAiCoreRuntimeHintsTests.java

Error fixed:

Error: An object of type 'org.slf4j.helpers.NOP_FallbackServiceProvider' was found in the image heap...
Error: An object of type 'org.slf4j.helpers.SubstituteServiceProvider' was found in the image heap...

Test Plan

  • Added unit tests for all registered types
  • Verified SLF4J types are properly registered
  • Test native compilation with Java 21
  • Test native compilation with Java 22
  • Ensure no regression in regular JVM mode

Fixes #494

@academey academey force-pushed the fix/issue-494-native-java22 branch 2 times, most recently from a5ee429 to 4473570 Compare July 17, 2025 01:24
@@ -0,0 +1,98 @@
/*
* Copyright 2023-2024 the original author or authors.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2025

academey added 2 commits July 18, 2025 17:26
This commit resolves the native compilation failure when using Java 22 with
GraalVM. The issue was caused by SLF4J service providers being initialized
at runtime instead of build time.

Changes:
- Add SLF4J RuntimeHints to SpringAiCoreRuntimeHints for native compilation
- Register NOP_FallbackServiceProvider and SubstituteServiceProvider for
  build-time initialization
- Add comprehensive tests for all registered types including SLF4J

This fix ensures compatibility with both Java 21 and Java 22 for native
image compilation.

Fixes spring-projects#494

Signed-off-by: academey <[email protected]>
- Update copyright year from 2023-2024 to 2023-2025
- Remove @author Christian Tzolov as per contributor guidelines

Signed-off-by: academey <[email protected]>
@academey academey force-pushed the fix/issue-494-native-java22 branch from cc9d20c to e2b2870 Compare July 18, 2025 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

native compilation under java 22 fails for an example that works on java 21
2 participants