bootJar cannot find the main class of a Spring Boot project
Main class name has not been configured and it could not be resolvedShort answer
The Spring Boot Gradle plugin looks in the main source set's output for a class with a public static void main method. If it finds none, or the entry point is a Kotlin file with a different generated class name, set mainClass yourself in the springBoot block.
Fix it fast
1Find out which cause you have
Confirm that the main source set really contains a class with a main method, and see which tasks the Spring Boot plugin added.
grep -rn "static void main\|fun main" src/main ./gradlew tasks --all | grep -i "boot"2Match what you see, then jump to its fix
What does the error look like?
Main class name has not been configured and it could not be resolved
Cause 1 of 2
No class with a main method in the main source set
1Check
The plugin searches the main source set's output for one class with public static void main(String[]). A module that has none gives it nothing to find.
find src/main -name "*Application*"2Fix
Name the class explicitly. Use the springBoot block to cover every task at once.
// build.gradle.kts
springBoot {
mainClass.set("com.example.ExampleApplication")
}
// build.gradle (Groovy)
springBoot {
mainClass = 'com.example.ExampleApplication'
}3Verify
Build the executable jar.
./gradlew bootJarConfirmed by docs.spring.io
Cause 2 of 2
The entry point is a Kotlin top-level function
1Check
Kotlin compiles a top-level main in ExampleApplication.kt into a class called ExampleApplicationKt, unless @JvmName changes it.
unzip -l build/libs/*.jar | grep -i "Application"2Fix
Use the generated class name, with the Kt suffix, in mainClass.
springBoot {
mainClass.set("com.example.ExampleApplicationKt")
}3Verify
Build and start the application from the jar.
./gradlew bootJar
java -jar build/libs/my-app-0.0.1-SNAPSHOT.jarConfirmed by docs.spring.io
How do I stop it coming back?
- Keep one class with the main method per runnable module, and set mainClass when a module has two.
- Check what a module is meant to be: a library module does not need an executable jar.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- Nuclear Home VideoA video-streaming Android app built from scratch, engineered for smooth playback.
Related fixes
Back end
A scanner flags jackson-databind: how to upgrade it in Gradle
jackson-databind vulnerability reported by a dependency scanner
Find where the old jackson-databind comes from, then raise it.
3 causes, each with a fixBack end
Gradle cannot resolve spring-boot-gradle-plugin after a Spring Boot 3 upgrade
Plugin [id: 'org.springframework.boot', version: '3.0.1'] was not found in any of the following sources
Spring Boot 3 needs Java 17 or newer and a recent Gradle, and its Gradle plugin is published on the Gradle Plugin Portal.
3 causes, each with a fixBack end
Spring Boot fails at startup with an error processing a condition on an auto-configuration
java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration
This message only says Spring Boot failed while evaluating an auto-configuration condition.
3 causes, each with a fixBack end
Swagger UI URL shows the Whitelabel Error Page in Spring Boot
Whitelabel Error Page when opening /swagger-ui.html
The Whitelabel page means Spring found no handler for that address.
3 causes, each with a fixBack end
Spring Boot will not start because port 8080 is already taken
Action: Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.
Spring Boot listens on 8080 unless told otherwise, and only one process can own a port.
3 causes, each with a fixBack end
Spring Boot cannot load the MySQL JDBC driver class
Cannot load driver class: com.mysql.jdbc.Driver
Spring Boot checks that the driver class you name can be loaded before it builds the connection pool.
3 causes, each with a fix