Spring Boot 3 Native Fails to Start with Kotlin @JvmStatic Main Method
See original GitHub issueBug 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.
Issue Analytics
- State:
- Created a year ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
Error: Main method is not static in class. Kotlin Project
If you don't want to declare the main function inside the App class you could just create an App.kt file with the following...
Read more >Spring Boot Reference Documentation
All of Spring Boot is open source, including the documentation. If you find problems with the docs or if you want to improve...
Read more >Calling Java from Kotlin
Existing Java code can be called from Kotlin in a natural way, and Kotlin code can be used from Java rather smoothly as...
Read more >Current State of Spring Boot 2.7 Native with Kotlin (GraalVM)
The JVM (Java Virtual Machine) that is used to run most server-side Kotlin programs is known for a slow start-up and warm-up time...
Read more >A: Mixing Kotlin with Java - The Joy of Kotlin
Of course, Gradle is also one of the best tools for managing Java projects, so you won't be surprised to learn that Gradle...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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.Got it. Thanks for the link. 🙏