Skip to content

Conversation

@academey
Copy link
Contributor

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
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
@ilayaperumalg
Copy link
Member

@joshlong Could you help with the review of the changes? Thanks

@joshlong
Copy link
Member

joshlong commented Sep 28, 2025

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?

@joshlong
Copy link
Member

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

@prashantguleria
Copy link

Same problem with Spring boot 4.0.0-M3

@academey
Copy link
Contributor Author

academey commented Oct 9, 2025

Hi @joshlong (it's really really really really big honor to me for mentioning you)
You're right. I investigated and confirmed that com.google.api:gax:2.65.0 (currently used viacom.google.cloud:libraries-bom:26.60.0) no longer contains org.slf4j.LoggerFactory in its native-image configuration. The upstream gax library has already fixed this issue.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

6 participants