Building and running web app as native image fails with java.lang.ClassNotFoundException after upgrade to Boot 3.1.0 from 3.0.5.

Sample project pom.xml for 3.0.x - https://github.com/trisberg/tanzu-java-web-app/blob/main/pom.xml Same after update to Boot 3.1 - https://github.com/trisberg/tanzu-java-web-app/blob/boot31/pom.xml

The exception:

2023-06-11T09:52:54.937486462-04:00 Exception in thread "main" java.lang.IllegalArgumentException: Unable to instantiate factory class [org.springframework.boot.devtools.restart.RestartScopeInitializer] for factory type [org.springframework.context.ApplicationContextInitializer]
2023-06-11T09:52:54.937538656-04:00     at org.springframework.core.io.support.SpringFactoriesLoader$FailureHandler.lambda$throwing$0(SpringFactoriesLoader.java:651)
2023-06-11T09:52:54.937546131-04:00     at org.springframework.core.io.support.SpringFactoriesLoader$FailureHandler.lambda$handleMessage$3(SpringFactoriesLoader.java:675)
2023-06-11T09:52:54.937551864-04:00     at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:231)
2023-06-11T09:52:54.937556622-04:00     at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:206)
2023-06-11T09:52:54.937561230-04:00     at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:160)
2023-06-11T09:52:54.937566131-04:00     at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:462)
2023-06-11T09:52:54.937570912-04:00     at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:458)
2023-06-11T09:52:54.937576500-04:00     at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:274)
2023-06-11T09:52:54.937581639-04:00     at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:253)
2023-06-11T09:52:54.937587087-04:00     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1305)
2023-06-11T09:52:54.937591930-04:00     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1294)
2023-06-11T09:52:54.937596813-04:00     at com.example.springboot.Application.main(Application.java:17)
2023-06-11T09:52:54.937601696-04:00 Caused by: java.lang.ClassNotFoundException: org.springframework.boot.devtools.restart.RestartScopeInitializer
2023-06-11T09:52:54.937609439-04:00     at java.base@17.0.6/java.lang.Class.forName(DynamicHub.java:1132)
2023-06-11T09:52:54.937614517-04:00     at org.springframework.util.ClassUtils.forName(ClassUtils.java:284)
2023-06-11T09:52:54.937619527-04:00     at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:224)
2023-06-11T09:52:54.937624712-04:00     ... 9 more

Comment From: trisberg

Possibly related issue https://github.com/spring-projects/spring-boot/issues/33760

Comment From: mhalbritter

Can be reproduced with a project from start.spring.io with devtools and native dependency. No other changes needed.

Comment From: ninijule

Hello,

Currently, I'm new to use GraalVm but i have exactly the same problem as @mhalbritter.

in the pom i got this for dev-tools.

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
````

I personally got this error

.\demo.exe Exception in thread "main" java.lang.IllegalArgumentException: Unable to instantiate factory class [org.springframework.boot.devtools.restart.RestartScopeInitializer] for factory type [org.springframework.context.ApplicationContextInitializer] at org.springframework.core.io.support.SpringFactoriesLoader$FailureHandler.lambda$throwing$0(SpringFactoriesLoader.java:651) at org.springframework.core.io.support.SpringFactoriesLoader$FailureHandler.lambda$handleMessage$3(SpringFactoriesLoader.java:675) at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:231) at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:206) at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:160) at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:462) at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:458) at org.springframework.boot.SpringApplication.(SpringApplication.java:274) at org.springframework.boot.SpringApplication.(SpringApplication.java:253) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1305) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1294) at fr.beutininformatique.backend.demo.BackEndApplication.main(BackEndApplication.java:12) Caused by: java.lang.ClassNotFoundException: org.springframework.boot.devtools.restart.RestartScopeInitializer at org.graalvm.nativeimage.builder/com.oracle.svm.core.hub.ClassForNameSupport.forName(ClassForNameSupport.java:123) at org.graalvm.nativeimage.builder/com.oracle.svm.core.hub.ClassForNameSupport.forName(ClassForNameSupport.java:87) at java.base@17.0.7/java.lang.Class.forName(DynamicHub.java:1322) at java.base@17.0.7/java.lang.Class.forName(DynamicHub.java:1311) at org.springframework.util.ClassUtils.forName(ClassUtils.java:284) at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:224) ... 9 more ```

don't know if it will help :|

Good luck !

Comment From: wilkinsona

The change in behavior is due to https://github.com/spring-projects/spring-boot/issues/35548. The changes made for that issue mean that DevTools is no longer included on the classpath during AOT processing. As a result, no reflection hints are generated for org.springframework.boot.devtools.restart.RestartScopeInitializer which is registered via spring.factories. We can't control the native image classpath so DevTools is present and is included in the image. This results in its spring.factories file being found and an attempt made to load org.springframework.boot.devtools.restart.RestartScopeInitializer. It fails due to the aforementioned lack of reflection hints.