spring-boot: Spring Boot 3 Native Fails to Start with Kotlin @JvmStatic Main Method
Bug Report for Spring Boot 3 Native (GraalVM 22.3) Having a simple Spring Boot application (generated on start.spring.io), changing the main class to:
@SpringBootApplication(proxyBeanMethods = false)
class DemoApplication {
companion object {
@JvmStatic
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}
}
}
And building the native image (via ./gradlew nativeCompile) gives you an executable that fails to start with:
2022-10-29T11:11:27.459+02:00 ERROR 8553 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalArgumentException: Could not find class [com.example.demo.DemoApplication$Companion__ApplicationContextInitializer]
at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:333) ~[na:na]
at org.springframework.context.aot.AotApplicationContextInitializer.instantiateInitializer(AotApplicationContextInitializer.java:80) ~[demo:6.0.0-RC2]
at org.springframework.context.aot.AotApplicationContextInitializer.initialize(AotApplicationContextInitializer.java:71) ~[demo:6.0.0-RC2]
at org.springframework.context.aot.AotApplicationContextInitializer.lambda$forInitializerClasses$0(AotApplicationContextInitializer.java:61) ~[demo:6.0.0-RC2]
at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:603) ~[demo:3.0.0-RC1]
at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:383) ~[demo:3.0.0-RC1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[demo:3.0.0-RC1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[demo:3.0.0-RC1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~[demo:3.0.0-RC1]
at com.example.demo.DemoApplication$Companion.main(DemoApplication.kt:15) ~[na:na]
at com.example.demo.DemoApplication.main(DemoApplication.kt) ~[demo:na]
Caused by: java.lang.ClassNotFoundException: com.example.demo.DemoApplication$Companion__ApplicationContextInitializer
at java.base@19.0.1/java.lang.Class.forName(DynamicHub.java:1132) ~[demo:na]
at org.springframework.util.ClassUtils.forName(ClassUtils.java:283) ~[na:na]
at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:323) ~[na:na]
... 10 common frames omitted
Let me know if you need more information. Thanks.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 5
- Comments: 32 (11 by maintainers)
@kkocel I played around with your
mainbranch to understand why it happens and noticed that when I only downgrade to Kotlin 1.8.22 your example builds and run without the exception.I made a project that reproduces this issue - it has two branches: one with Kotlin main and another with Java main. Both fail with the same error. https://github.com/kkocel/classnotfoundrepero
Got it. Thanks for the link. 🙏
@kkocel thanks, but, as far as I can tell, the issue as originally reported still occurs with Spring Boot 3.2 and Kotlin 1.9.20. In fact, it’s got slightly worse as the error message is no longer as helpful as it was:
I think we should consider refining or reverting https://github.com/spring-projects/spring-boot/pull/38188, particularly while this issue is unresolved.
Have the same problem. No luck with workarounds that were mentioned here.
This shouldn’t be necessary.
bootRunshould be automatically configured to use the right main class when using either a companion object or a package-level function.