question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spring Boot 3 Native Fails to Start with Kotlin @JvmStatic Main Method

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
wilkinsonacommented, Nov 1, 2022

The workaround doesn’t work for me since I have to set the main class in bootRun task

This shouldn’t be necessary. bootRun should be automatically configured to use the right main class when using either a companion object or a package-level function.

1reaction
akefiradcommented, Oct 31, 2022

Got it. Thanks for the link. 🙏

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found