Unable to run Kotlin app from IntelliJ


#1

Even though I can run a Spring boot app via java -jar, I get this error when I try to run it from IntelliJ:

fun main(vararg args: String) =
    SpringApplication.run(Application::class.java, *args)

main method must return a value of type void


#2

When you use the expression body syntax, the return type of the main() function will be the same as the type of its body expression. In your example, SpringApplication.run returns an instance of ConfigurableApplicationContext, so your main function will also have that return type. This doesn’t match the signature of main required by the JVM.

The easiest fix for the problem is to change the main function to use the block body syntax with no return statement.


#3

Thanks for clarifying the change of return type.

However, I still don’t understand why I can run the same main method via java -jar <path-to-jar>.

It looks as if the JVM only cares about the method signature (which encompasses the method name and the types and order of its arguments, but doesn’t include argument names nor the method return type).

If it isn’t a limitation of the JVM, then it looks like it’s some kind of limitation of IntelliJ.


#4

A signature of a Java method does include its return type, and the main method is required to return void. I have no idea why you can run the app through java -jar.