{"id":"6d69a7fdd92c09c6","repo":"spring-projects/spring-boot","slug":"failed-to-read-main-class-name-from","errorCode":null,"errorMessage":"Failed to read main class name from '{}'","messagePattern":"Failed to read main class name from '(.+?)'","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"build-plugin/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/ResolveMainClassName.java","lineNumber":184,"sourceCode":"\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":166,"sourceCodeEnd":191,"githubUrl":"https://github.com/spring-projects/spring-boot/blob/5b2dbdbb8be64415eb6552f81ff7c449c8d251e6/build-plugin/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/ResolveMainClassName.java#L166-L191","documentation":"Thrown by ResolveMainClassName.ClassNameReader.transform() when Files.readString on the main-class-name output file raises IOException, wrapped as a RuntimeException (note: the cause is not chained). It indicates the file that resolveMainClassName previously wrote could not be read back when the provider is queried lazily.","triggerScenarios":"readMainClassName() maps outputFile through ClassNameReader; at line 181 Files.readString(output) fails (file deleted mid-build, moved, permission denied, FS error), producing the RuntimeException at line 184.","commonSituations":"Another task or external process deleted build/ intermediate files between resolveMainClassName and the consumer task; concurrent Gradle builds sharing a build dir; antivirus/IDE file locking on Windows; running with --parallel and a cleaned build dir; stale build after an interrupted build.","solutions":["Run a clean build: ./gradlew clean bootJar to regenerate the main-class-name file.","Ensure no external process (IDE, antivirus, another Gradle daemon) removes files under build/ during the build.","Avoid sharing a single build directory across parallel Gradle invocations.","If reproducible, inspect the file path in the message and check filesystem permissions/disk health."],"exampleFix":"// shell: force regeneration of the resolveMainClassName output\n//   ./gradlew clean resolveMainClassName bootJar\n","handlingStrategy":"try-catch","validationCode":"// Ensure the resolveMainClassName output exists and is readable before consuming\nval outFile = layout.buildDirectory.file(\"resolvedMainClassName\").get().asFile\nif (!outFile.exists() || outFile.length() == 0L) {\n    throw GradleException(\"resolveMainClassName output missing; run :resolveMainClassName\")\n}\n","typeGuard":null,"tryCatchPattern":"try {\n    val mainClass = tasks.named<ResolveMainClassName>(\"resolveMainClassName\").get().readMainClassName().get()\n} catch (ex: RuntimeException) {\n    if (ex.message?.startsWith(\"Failed to read main class name from\") == true) {\n        logger.lifecycle(\"Re-running resolveMainClassName to regenerate output\")\n        tasks.named(\"resolveMainClassName\").get().actions.forEach { /* re-trigger */ }\n    }\n    throw ex\n}\n","preventionTips":["Run ./gradlew clean when builds are interrupted to avoid stale main-class files.","Do not let external processes delete files under build/.","Avoid running multiple Gradle builds against a shared build directory in parallel."],"tags":["gradle","main-class","io","build-state","race-condition"],"analyzedSha":"5b2dbdbb8be64415eb6552f81ff7c449c8d251e6","analyzedAt":"2026-08-04T18:53:14.967Z","schemaVersion":2}