Spring-boot: Cannot start SpringBoot2 app (written in Kotlin) using gradle bootRun

Created on 24 Jun 2018  路  5Comments  路  Source: spring-projects/spring-boot

I have got spring boot 2 application written in Kotlin. When I am running the SpringBoot App.kt class via IntelliJ it starts up correctly.

While running the same application via ./gradlew bootRun keep getting following error:

```> Task :app:bootRun FAILED
Error: Main method not found in class test.App, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':app:bootRun'.
    > Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
Following are the versions used:
**Gradle version:** 4.8.1
**SpringBoot version:** 2.0.3.RELEASE
**Kotlin version:** 1.2.50


Following is the code snippet of the **App.kt** class

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer

@SpringBootApplication
class App : SpringBootServletInitializer()

fun main(args: Array) {
runApplication(*args)
}
```

Could someone please help me with the issue.

Thanks & Regards,
Sanjay

invalid

Most helpful comment

Thanks for the sample.

bootRun is failing when you configure mainClassName as the name is incorrect. As you're using Kotlin, Kt is added to the name of the class when it's compiled. As a result, you need to use AppKt. Also, App is in the test package whereas you've used au.com.test. The correct configuration is the following:

mainClassName="test.AppKt"

With this change in place, both bootRun and build will work.

Without configuring mainClassName the startScripts task fails. This is a standard task created when you apply the Application plugin. As noted in its documentation, configuring mainClassName is mandatory.

bootRun and bootJar do not require an explicitly configured main class as Boot's plugin contains some logic to figure it out. The application's plugin's startScripts task has no such logic.

All 5 comments

Thanks for the report, but I can't reproduce the behaviour using the small snippet of code that you have provided. Perhaps there's something in your build.gradle or elsewhere that's causing it?

If you would like us to spend some more time investigating, please take the time to provide a complete, minimal sample (something that we can unzip or git clone and run) that reproduces the problem.

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@wilkinsona Following is the git repo with the issue: https://github.com/sanjaybhatol/springboot2BootRunIssueApp

Please note below:

code snippet from build.gradle file
//NOTE: // when you comment out the mainClassName './gradlew bootRun` command works but './gradlew clean build' command FAILS. // when you keep mainClassName './gradlew clean build' will work but './gradlew bootRun' fails. mainClassName="au.com.test.App"

Thanks for the sample.

bootRun is failing when you configure mainClassName as the name is incorrect. As you're using Kotlin, Kt is added to the name of the class when it's compiled. As a result, you need to use AppKt. Also, App is in the test package whereas you've used au.com.test. The correct configuration is the following:

mainClassName="test.AppKt"

With this change in place, both bootRun and build will work.

Without configuring mainClassName the startScripts task fails. This is a standard task created when you apply the Application plugin. As noted in its documentation, configuring mainClassName is mandatory.

bootRun and bootJar do not require an explicitly configured main class as Boot's plugin contains some logic to figure it out. The application's plugin's startScripts task has no such logic.

Read this: http://engineering.pivotal.io/post/spring-boot-application-with-kotlin/

My problem was ./gradlew bootRun doesn't work because the public static void main wasn't found.
things to do:
1.-define an springboot Kotlin file

`
@SpringBootApplication
class Application

fun main(args: Array) {
runApplication(*args)
}
`

2.-setup the mainClassName property in the build.gradle file.
mainClassName = yourPackageName. ApplicationKt

So if you define the main method as a function, you have to add the suffix 'Kt' to the name of the class in the build.gradle file.

In my example, Kotlin compiles the Application file in two different files:

  • one file called Application.class with the Springboot things
  • another file called ApplicationKt.class with the main method

In this second file is where the main function is located at, so you have to use this name in the build.gradle file.

Was this page helpful?
0 / 5 - 0 ratings