spring-projects/spring-boot · error · MojoExecutionException

Unable to find a suitable main class, please add a…

Error message

Unable to find a suitable main class, please add a 'mainClass' property

What it means

SpringBootApplicationClassFinder.findSingleClass iterates each classesDirectory and uses MainClassFinder to look for a class annotated with @SpringBootApplication. If none is found across all directories, it throws MojoExecutionException instructing the user to add a 'mainClass' property to the plugin configuration.

Solutions

  1. Set <configuration><mainClass>com.example.MyApp</mainClass></configuration> in the plugin config
  2. Annotate the entry class with @SpringBootApplication (or ensure compile has produced classes)
  3. For multi-module builds, ensure the module containing the main class is built first

Example fix

<!-- before -->
<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin>
<!-- after -->
<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.example.Application</mainClass></configuration></plugin>
Defensive patterns

Strategy: validation

Validate before calling

// Scan the classes directory for @SpringBootApplication before invoking the goal
File classesDir = new File(project.getBuild().getOutputDirectory());
String main = org.springframework.boot.loader.tools.MainClassFinder.findSingleMainClass(classesDir,
    "org.springframework.boot.autoconfigure.SpringBootApplication");
if (main == null && explicitMainClass == null) { /* configure mainClass */ }

Try / catch

try { finder.findSingleClass(dirs); } catch (MojoExecutionException ex) { log.error("Set <mainClass> in plugin config or annotate the entry class with @SpringBootApplication"); throw ex; }

Prevention

When it happens

Trigger: Running spring-boot:run or spring-boot:repackage on a project whose compiled classes contain no @SpringBootApplication-annotated type, and no mainClass is configured.

Common situations: Multi-module build where the main class lives in a sibling module; main class uses @EnableAutoConfiguration + @ComponentScan + @Configuration instead of @SpringBootApplication; classes directory not yet compiled; test scope only.

Related errors


AI-assisted analysis of spring-projects/spring-boot@270dfe353f (2026-08-11). Data as JSON: /api/errors/9318a06ae62b3e1a. Report an issue: GitHub.

Appendix: source

Thrown at build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/SpringBootApplicationClassFinder.java:50

 */
abstract class SpringBootApplicationClassFinder {

	private static final String SPRING_BOOT_APPLICATION_CLASS_NAME = "org.springframework.boot.autoconfigure.SpringBootApplication";

	static String findSingleClass(File classesDirectory) throws MojoExecutionException {
		return findSingleClass(List.of(classesDirectory));
	}

	static String findSingleClass(List<File> classesDirectories) throws MojoExecutionException {
		try {
			for (File classesDirectory : classesDirectories) {
				String mainClass = MainClassFinder.findSingleMainClass(classesDirectory,
						SPRING_BOOT_APPLICATION_CLASS_NAME);
				if (mainClass != null) {
					return mainClass;
				}
			}
			throw new MojoExecutionException("Unable to find a suitable main class, please add a 'mainClass' property");
		}
		catch (IOException ex) {
			throw new MojoExecutionException(ex.getMessage(), ex);
		}
	}

}

View on GitHub (pinned to 270dfe353f)