Hi all! please tell me why? Spring Boot Applcation...
# getting-started
s
Hi all! please tell me why? Spring Boot Applcation Main class (for start) can not start, application
Copy code
@SpringBootApplication
class ApiApplication {

    fun main(args: Array<String>) {
        runApplication<ApiApplication>(*args) {
            setBannerMode(Banner.Mode.OFF)
        }
    }
}
and this work! why?
Copy code
@SpringBootApplication
class ApiApplication {

    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            SpringApplication.run(ApiApplication::class.java, *args)
        }
    }

}
k
In your first example,
main
is not one of the ways to specify an entry point in Kotlin. As you have it, it's just an ordinary instance function of the class. You can fix it by moving it out of the class:
Copy code
@SpringBootApplication
class ApiApplication

fun main(args: Array<String>) {
    runApplication<ApiApplication>(*args) {
        setBannerMode(Banner.Mode.OFF)
    }
}
1
s
thank you!
I didn’t notice
magic)
v
To add a bit more context, If you compare this with the entry point in a Java class, you could see a
public
and
static
keyword as part of the main method signature. Your second example example get translated to this because of that companion object and static annotation, and that's why it worked. In case of Kotlin, the entry point is a top level function (without enclosing that inside a class). And like @Klitos Kyriacou said, when you wrap it inside a class, it becomes an ordinary instance method. Also,
runApplication<ApiApplication>(*args)
is just a Kotlin idiomatic alternative to
SpringApplication.run(ApiApplication::class.java, *args)
👌🏻 1