{"record":{"id":"9318a06ae62b3e1a","repo":"spring-projects/spring-boot","slug":"unable-to-find-a-suitable-main-class-please-add-a","errorCode":null,"errorMessage":"Unable to find a suitable main class, please add a 'mainClass' property","messagePattern":"Unable to find a suitable main class, please add a 'mainClass' property","errorType":"exception","errorClass":"MojoExecutionException","httpStatus":null,"severity":"error","filePath":"build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/SpringBootApplicationClassFinder.java","lineNumber":50,"sourceCode":" */\nabstract class SpringBootApplicationClassFinder {\n\n\tprivate static final String SPRING_BOOT_APPLICATION_CLASS_NAME = \"org.springframework.boot.autoconfigure.SpringBootApplication\";\n\n\tstatic String findSingleClass(File classesDirectory) throws MojoExecutionException {\n\t\treturn findSingleClass(List.of(classesDirectory));\n\t}\n\n\tstatic String findSingleClass(List<File> classesDirectories) throws MojoExecutionException {\n\t\ttry {\n\t\t\tfor (File classesDirectory : classesDirectories) {\n\t\t\t\tString mainClass = MainClassFinder.findSingleMainClass(classesDirectory,\n\t\t\t\t\t\tSPRING_BOOT_APPLICATION_CLASS_NAME);\n\t\t\t\tif (mainClass != null) {\n\t\t\t\t\treturn mainClass;\n\t\t\t\t}\n\t\t\t}\n\t\t\tthrow new MojoExecutionException(\"Unable to find a suitable main class, please add a 'mainClass' property\");\n\t\t}\n\t\tcatch (IOException ex) {\n\t\t\tthrow new MojoExecutionException(ex.getMessage(), ex);\n\t\t}\n\t}\n\n}\n","sourceCodeStart":32,"sourceCodeEnd":58,"githubUrl":"https://github.com/spring-projects/spring-boot/blob/270dfe353fb830fd69b823a8a859287ff103854b/build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/SpringBootApplicationClassFinder.java#L32-L58","documentation":"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.","triggerScenarios":"Running spring-boot:run or spring-boot:repackage on a project whose compiled classes contain no @SpringBootApplication-annotated type, and no mainClass is configured.","commonSituations":"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.","solutions":["Set <configuration><mainClass>com.example.MyApp</mainClass></configuration> in the plugin config","Annotate the entry class with @SpringBootApplication (or ensure compile has produced classes)","For multi-module builds, ensure the module containing the main class is built first"],"exampleFix":"<!-- before -->\n<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin>\n<!-- after -->\n<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.example.Application</mainClass></configuration></plugin>","handlingStrategy":"validation","validationCode":"// Scan the classes directory for @SpringBootApplication before invoking the goal\nFile classesDir = new File(project.getBuild().getOutputDirectory());\nString main = org.springframework.boot.loader.tools.MainClassFinder.findSingleMainClass(classesDir,\n    \"org.springframework.boot.autoconfigure.SpringBootApplication\");\nif (main == null && explicitMainClass == null) { /* configure mainClass */ }","typeGuard":null,"tryCatchPattern":"try { finder.findSingleClass(dirs); } catch (MojoExecutionException ex) { log.error(\"Set <mainClass> in plugin config or annotate the entry class with @SpringBootApplication\"); throw ex; }","preventionTips":["Always set <mainClass> explicitly in multi-module builds","Use @SpringBootApplication on a single entry class per module","Ensure compile has run before invoking run/repackage goals"],"tags":["maven-plugin","configuration","main-class"],"backgroundTag":null,"analyzedSha":"270dfe353fb830fd69b823a8a859287ff103854b","analyzedAt":"2026-08-11T19:42:06.541Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}