Fix Spring Boot Unable to Find a Suitable Main Class
Fix the Maven and Gradle error about a missing main class by repackaging only the runnable module or by naming the main class explicitly.

The message Unable to find a suitable main class, please add a 'mainClass' property comes from the Spring Boot build plugin, not from the compiler and not from Spring at run time. The plugin repackages your jar into an executable one, and to do that it has to know which class starts the application. It scans the compiled classes for one with a public static void main. If it finds none, or finds several and cannot choose, it stops. This post lists the four situations that produce the error, in order of how often they occur, and gives the build change for Maven and Gradle in each. It applies to Spring Boot 3 and 4 and JDK 17 or later.
What the error looks like
In Maven it appears at the repackage goal:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:4.0.0:repackage
(repackage) on project shop-domain: Execution repackage of goal ...:repackage failed:
Unable to find main classand in Gradle at bootJar or bootRun:
Execution failed for task ':shop-domain:bootJar'.
> Main class name has not been configured and it could not be resolvedThe module named in the message is the one that has no main class. Read it before changing anything: the fix is almost always in that module, or in the parent that gave it the plugin.
Cause one: the plugin is applied to a library module
A multi-module build has one runnable module and several libraries. The plugin should be applied only to the runnable one. It lands on the libraries by inheritance: a parent POM that declares the spring-boot-maven-plugin in <build><plugins> rather than in <pluginManagement> activates it in every child.
<!-- wrong: every module is repackaged -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>Move it to pluginManagement in the parent, and declare it in the application module only:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build><build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>If the parent comes from spring-boot-starter-parent, the plugin is already in its pluginManagement with repackage bound, but only for modules that declare the plugin. A library that lists the plugin by habit triggers the error. If you cannot remove the plugin from a library, skip the goal: <configuration><skip>true</skip></configuration> on the plugin in that module.
Cause two: Gradle builds a boot jar for a library
Applying org.springframework.boot to a library project registers bootJar, which needs a main class. Libraries should publish a plain jar. Turn one off and the other on:
tasks.bootJar { enabled = false }
tasks.jar { enabled = true }A cleaner layout applies the Boot plugin only to the application project and uses the dependency management plugin (io.spring.dependency-management) or a platform dependency in libraries. Then there is no bootJar task to disable. Library modules that still need Boot's dependency versions can import the BOM with implementation(platform(SpringBootPlugin.BOM_COORDINATES)).
Cause three: the module has no main method
Sometimes the module really is the application and the class is missing, empty or in the wrong place. Check that the class annotated @SpringBootApplication has public static void main(String[] args), that it compiled (look under target/classes or build/classes), and that the source is in src/main/java, not src/test/java. A class in the default package is found, but it breaks component scanning, which is its own problem. After a refactor that moved the application class to another module, the old module keeps the plugin and no longer has a class for it. The same module-split issues appear in application failed to start.
Cause four: more than one main method
The plugin picks the class if exactly one candidate exists. Tools, demos and CommandLineRunner entry points often add a second main. Name the one to use:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.example.shop.ShopApplication</mainClass>
</configuration>
</plugin>and in Gradle:
springBoot { mainClass.set("com.example.shop.ShopApplication") }An explicit name is also faster, since the plugin does not scan, and it survives someone adding a test utility with its own main in src/main. For jars built with the -Pnative profile or other plugins that also resolve the main class, the same property is read.
A quick way to find which module is at fault
When the build log is long, ask Maven for the module order and the plugin bindings instead of reading it:
./mvnw help:effective-pom -pl shop-domain | grep -B2 -A8 spring-boot-maven-pluginIf the plugin shows up in the effective POM of a module that should be a plain library, the declaration is inherited, and the output tells you from which parent. In Gradle, ./gradlew :shop-domain:tasks --all | grep boot lists bootJar and bootRun for the module, and their presence is the same signal. Fixing it in the one parent is better than patching each child, because the next library added to the build would otherwise fail the same way.
The error also appears after changing a module's packaging. A module that was jar and became pom (an aggregator) has no classes at all, so a plugin that stayed in its build has nothing to scan. Remove the plugin from aggregator POMs entirely.
Why the plugin insists on a main class
The repackage step rewrites the jar into the layout Boot's launcher understands: your classes under BOOT-INF/classes, dependencies under BOOT-INF/lib, and a manifest that points the JVM at JarLauncher and records your class as Start-Class. Without a start class the manifest would be incomplete, and the jar would build and then fail at run time with a less helpful message. The plugin chooses to fail early. That is why the fix is never to ignore the error, only to decide which modules are applications.
Check the result
Run the build for the failing module only and confirm what it produced.
./mvnw -pl shop-app -am package
unzip -p shop-app/target/shop-app-1.0.jar META-INF/MANIFEST.MF | grep -i "start-class\|main-class"A Boot jar has Main-Class: org.springframework.boot.loader.launch.JarLauncher and Start-Class set to your application class. A library jar has neither. If a library jar contains BOOT-INF, the plugin ran on it and Cause one still applies. For the build basics behind all of this, see Maven for beginners, and for a complete project layout, the first Spring Boot project.
Kotlin and generated sources
If the application class is in Kotlin, the main function may be top-level and compile to a class named ShopApplicationKt. Boot finds it, but the name you write in mainClass must be the compiled one, with the Kt suffix. Generated sources from annotation processors can also add a class with a main method in rare cases, which brings back Cause four.
When the message means something else
If the same text appears when you run from the IDE, the run configuration has no main class selected. Pick one in the configuration dialog; the build plugins are not involved. The Maven and Gradle plugin options are in the Maven plugin documentation and the Gradle plugin documentation.


