- 
                Notifications
    You must be signed in to change notification settings 
- Fork 2k
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
base: main
Are you sure you want to change the base?
Fix GraalVM native compilation issue with Java 22 #3833
Conversation
a5ee429    to
    4473570      
    Compare
  
            
          
                spring-ai-model/src/test/java/org/springframework/ai/aot/SpringAiCoreRuntimeHintsTests.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
      1e06a4b    to
    cc9d20c      
    Compare
  
    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]>
cc9d20c    to
    e2b2870      
    Compare
  
    | @joshlong Could you help with the review of the changes? Thanks | 
| hi - thanks for submitting ths. unfortunately im not sure how to break the code using java 23, 24, and 25 and modern versions of spring ai. the bug against which the original issue was filed was from march of 2024. would you mind sending an example of something that breaks and which then works if you add the hints you show in the PR? | 
| if i go to the spring initializr, choose java 25, maven, gemini, and web, then add package com.example.pr;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class PrApplication {
    public static void main(String[] args) {
        SpringApplication.run(PrApplication.class, args);
    }
    @Bean
    ApplicationRunner runner(ChatClient.Builder builder) {
        return args -> {
            var res = builder.build().prompt("tell me a joke").call().content();
            System.out.println(res);
        };
    }
}this seems to work just fine. not sure what error im looking for | 
| Same problem with Spring boot 4.0.0-M3 | 
| Hi @joshlong (it's really really really really big honor to me for mentioning you) Regarding the Spring Boot 4.0.0-M3 comment @prashantguleria, Could you share the specific error you encountered? This would help determine ifthere's a different issue, since my tests show the problem is already resolved in the current dependency versions. Unless we can reproduce the issue with the current dependencies, this PR appears unnecessary. | 
Summary
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_FallbackServiceProviderandSubstituteServiceProvider) were being initialized at runtime instead of build time in GraalVM native images.Changes made:
SpringAiCoreRuntimeHints.javato register SLF4J classes for reflection and build-time initializationSpringAiCoreRuntimeHintsTests.javaError fixed:
Test Plan
Fixes #494