spring-projects/spring-boot · error · InvalidUserDataException

Main class name has not been configured and it could not be…

Error message

Main class name has not been configured and it could not be resolved from classpath {}

What it means

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.

Solutions

  1. Set the main class explicitly: springBoot { mainClass = 'com.example.MyApplication' } or mainClass.set(...) in the application block.
  2. Add a public class with 'public static void main(String[] args)' to your main source set and ensure it compiles.
  3. If this is a library module, remove the 'application' or 'org.springframework.boot' plugin from that module.
  4. Run 'gradle classes --info' to see what the main-class resolver is scanning and confirm the class is on the runtime classpath.

Example fix

// before: no main class configured, resolver finds nothing
plugins {
    id 'org.springframework.boot'
}
// after: explicitly set main class
springBoot {
    mainClass = 'com.example.demo.DemoApplication'
}
Defensive patterns

Strategy: validation

Validate before calling

// Before any boot task runs, assert that a main class is resolvable.
def mainClassProp = project.extensions.getByType(SpringBootExtension.class).mainClass
if (!mainClassProp.isPresent()) {
    // attempt a manual scan; if none found, set explicitly
    mainClassProp.set('com.example.MyApplication')
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


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

Appendix: source

Thrown at build-plugin/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/ResolveMainClassName.java:175

			.getFiles()
			.stream()
			.map(File::getAbsolutePath)
			.collect(Collectors.joining(File.pathSeparator));
		return this.outputFile.map(new ClassNameReader(classpath));
	}

	private static final class ClassNameReader implements Transformer<String, RegularFile> {

		private final String classpath;

		private ClassNameReader(String classpath) {
			this.classpath = classpath;
		}

		@Override
		public String transform(RegularFile file) {
			if (file.getAsFile().length() == 0) {
				throw new InvalidUserDataException(
						"Main class name has not been configured and it could not be resolved from classpath "
								+ this.classpath);
			}
			Path output = file.getAsFile().toPath();
			try {
				return Files.readString(output);
			}
			catch (IOException ex) {
				throw new RuntimeException("Failed to read main class name from '" + output + "'");
			}
		}

	}

}

View on GitHub (pinned to 270dfe353f)