{"record":{"id":"e48523a9b6454f07","repo":"spring-projects/spring-boot","slug":"main-class-name-has-not-been-configured-and-it-cou","errorCode":null,"errorMessage":"Main class name has not been configured and it could not be resolved from classpath {}","messagePattern":"Main class name has not been configured and it could not be resolved from classpath (.+?)","errorType":"exception","errorClass":"InvalidUserDataException","httpStatus":null,"severity":"error","filePath":"build-plugin/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/ResolveMainClassName.java","lineNumber":175,"sourceCode":"\t\t\t.getFiles()\n\t\t\t.stream()\n\t\t\t.map(File::getAbsolutePath)\n\t\t\t.collect(Collectors.joining(File.pathSeparator));\n\t\treturn this.outputFile.map(new ClassNameReader(classpath));\n\t}\n\n\tprivate static final class ClassNameReader implements Transformer<String, RegularFile> {\n\n\t\tprivate final String classpath;\n\n\t\tprivate ClassNameReader(String classpath) {\n\t\t\tthis.classpath = classpath;\n\t\t}\n\n\t\t@Override\n\t\tpublic String transform(RegularFile file) {\n\t\t\tif (file.getAsFile().length() == 0) {\n\t\t\t\tthrow new InvalidUserDataException(\n\t\t\t\t\t\t\"Main class name has not been configured and it could not be resolved from classpath \"\n\t\t\t\t\t\t\t\t+ this.classpath);\n\t\t\t}\n\t\t\tPath output = file.getAsFile().toPath();\n\t\t\ttry {\n\t\t\t\treturn Files.readString(output);\n\t\t\t}\n\t\t\tcatch (IOException ex) {\n\t\t\t\tthrow new RuntimeException(\"Failed to read main class name from '\" + output + \"'\");\n\t\t\t}\n\t\t}\n\n\t}\n\n}\n","sourceCodeStart":157,"sourceCodeEnd":191,"githubUrl":"https://github.com/spring-projects/spring-boot/blob/270dfe353fb830fd69b823a8a859287ff103854b/build-plugin/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/ResolveMainClassName.java#L157-L191","documentation":"Thrown by ResolveMainClassName.ClassNameReader.transform when the RegularFile that should contain the resolved main class name has zero length. This means the upstream main-class detection task (which writes the class name to this temp file) found no class with a main method on the classpath, and no mainClassName was explicitly configured by the user. InvalidUserDataException signals a user-configuration problem.","triggerScenarios":"A Spring Boot Gradle project where the bootJar or related task needs a main class, the springBoot.mainClass property is unset, and the classpath scan produced no public class with 'public static void main(String[])'. The output file from the resolution task exists but is empty (length == 0).","commonSituations":"A multi-module project where the application module has no main class yet (still a library). A project where the main class was deleted or renamed without updating configuration. A project using Kotlin/Gradle mixed sources where the main class is not on the classpath visible to the resolver. A test or buildSrc module that applies the application plugin without a real entry point.","solutions":["Set the main class explicitly: springBoot { mainClass = 'com.example.MyApplication' } or mainClass.set(...) in the application block.","Add a public class with 'public static void main(String[] args)' to your main source set and ensure it compiles.","If this is a library module, remove the 'application' or 'org.springframework.boot' plugin from that module.","Run 'gradle classes --info' to see what the main-class resolver is scanning and confirm the class is on the runtime classpath."],"exampleFix":"// before: no main class configured, resolver finds nothing\nplugins {\n    id 'org.springframework.boot'\n}\n// after: explicitly set main class\nspringBoot {\n    mainClass = 'com.example.demo.DemoApplication'\n}","handlingStrategy":"validation","validationCode":"// Before any boot task runs, assert that a main class is resolvable.\ndef mainClassProp = project.extensions.getByType(SpringBootExtension.class).mainClass\nif (!mainClassProp.isPresent()) {\n    // attempt a manual scan; if none found, set explicitly\n    mainClassProp.set('com.example.MyApplication')\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set springBoot.mainClass explicitly in multi-module builds.","Ensure at least one class on the main source set has 'public static void main(String[])'.","Do not apply the application/spring-boot plugin to pure-library modules."],"tags":["gradle","main-class","configuration","spring-boot-plugin","application-plugin"],"backgroundTag":null,"analyzedSha":"270dfe353fb830fd69b823a8a859287ff103854b","analyzedAt":"2026-08-11T19:42:06.541Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}