SpringBoot Unable to find a single main class from the following candidates
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Spring Boot is a robust framework that simplifies the process of setting up and developing new Spring applications. It is widely used for building stand-alone, production-grade Spring-based applications quickly, avoiding complex configuration settings. However, despite its user-friendly approach, developers might encounter warnings or errors during the development process. One such issue that developers face is the "Unable to find a single main class from the following candidates."
This article explains this particular issue, delving into its causes, solutions, and preventive measures.
Understanding the Error
When you encounter this error, it means Spring Boot is unable to determine a single main class to use as the entry point of the application. Here are the major causes:
- Multiple Main Classes: If you have multiple classes with a
public static void main(String[] args)method, Spring Boot will be confused about which one to use. - Manifest Misconfiguration: Sometimes this issue can arise if the
MANIFEST.MFfile in your JAR does not have theMain-Classattribute correctly specified. - Invalid Project Structure: The project might be incorrectly structured, or the main method could be missing in the identified class files.
Locating the Main Class
A Spring Boot application typically starts executing from a class with the main method. Here's an example:
Scenario with Multiple Main Classes
Consider a scenario where multiple Java files contain a main method:
In such cases, Spring Boot cannot determine which class to prioritize and throws an error. The remedy is to specify explicitly which main class should be used.
Modifying the pom.xml or build.gradle
For a Maven project, you can set the start-class property in your pom.xml:
If you are using Gradle, add the following in your build.gradle:
Correcting the Manifest File
Check and correct your JAR’s MANIFEST.MF file to specify the main class:
Preventive Measures
- Maintain One Main Class: To avoid ambiguity, maintain only one main application class in a given module.
- Configure Correct Builds: Ensure your build configuration files (
pom.xmlorbuild.gradle) are correctly set up to declare the main class. - Use Spring Boot Annotations: Use the
@SpringBootApplicationannotation properly, which encompasses@Configuration,@EnableAutoConfiguration, and@ComponentScan.
Conclusion
Encountering the "Unable to find a single main class from the following candidates" error in a Spring Boot project is primarily a configuration issue. By adopting best practices such as maintaining a single main class per module and correctly setting up build files, this problem can be easily mitigated. Ensuring clarity over entry point configuration can prevent further deployment and execution issues, facilitating a smoother development experience.
Summary Table
| Cause | Solution |
| Multiple main classes | Specify the main class explicitly using pom.xml (Maven) or build.gradle (Gradle) |
| Manifest misconfiguration | Set the Main-Class in the MANIFEST.MF file |
| Invalid project structure | Ensure proper structure and existence of main method in entry class |
| Preventive measure (Ambiguity avoidance) | Maintain one main class per module |
These insights and actions can effectively address the problem, allowing you to leverage the power of Spring Boot with minimal configuration hiccups.
Related reading
- Springboot Wildfly 10 deployment error jdk.unsupported module not found
- SpringBoot with LogBack creating LOG_PATH_IS_UNDEFINED folder
- SpringBoot's MultipartConfig maxFileSize not taking effect
- SpringBootTest No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available
- springboot Upgrade from 2.3.5.RELEASE to 2.4.1- ClassNotFoundException org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
- Springfox 3.0.0 is not working with Spring Boot 2.6.0
- SpringBootTest vs ContextConfiguration vs Import in Spring Boot Unit Test
- SpringBootTest Vs WebMvcTest DataJpaTest service unit tests, what is the best?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.