Spring Boot Program cannot find main class
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Understanding and Solving "Spring Boot Program Cannot Find Main Class"
Spring Boot is a popular framework that simplifies the development of Java applications. However, developers might occasionally encounter the error "Cannot find main class" when executing a Spring Boot application. This can be frustrating, especially for beginners. Below, we unravel the common reasons for this error and provide detailed steps to solve the issue.
What Causes the "Cannot Find Main Class" Error?
Several reasons could trigger this issue. Here are some of the most common factors:
- Incorrect Classpath Configuration: The application might not be able to locate the main class if the classpath is misconfigured.
- Incorrect Manifest File: Spring Boot uses a
META-INF/MANIFEST.MFfile to determine the main class in a jar file. An incorrect or missing entry can cause the error. - Package Structure Issues: An incorrect or improperly declared package structure can lead to this issue.
- Build Tool Misconfiguration: Tools like Maven or Gradle may be improperly configured, leading to an inability to locate the main method.
- IDE Configuration Mistakes: Incorrect IDE settings might fail to locate and run the main class.
Solving the Error
To address this problem, consider the following solutions:
1. Verify the Classpath
Ensure that the classpath includes all necessary directories and libraries. Incorrect classpaths often cause the application to fail in locating the main class.
- Example Command:
2. Check the Manifest File
Ensure that the MANIFEST.MF file correctly specifies the main class. The Main-Class entry should match the expected package and class name.
- Example:
3. Review Your Package Structure
Ensure that the package declaration in your Java classes matches the directory structure. The package name declared in the MainApp.java file should match the directory structure used.
4. Validate Build Tool Configuration
For Maven users, ensure that the pom.xml includes the following in the <build> section:
For Gradle users, ensure the build.gradle includes the Spring Boot plugin:
5. Adjust IDE Settings
Verify that your IDE is properly set up to recognize the main class:
- Ensure that the run configurations specify the correct main class.
- Clean and rebuild the project to ensure all changes take effect.
Key Points Summary
| Issue | Solution |
| Incorrect classpath configuration | Ensure CLASSPATH includes all necessary libraries. |
Incorrect MANIFEST.MF | Verify Main-Class entry is correct. |
| Package structure issues | Align package declarations with directory structure. |
| Build tool misconfiguration (Maven) | Ensure Spring Boot plugin is set in pom.xml. |
| Build tool misconfiguration (Gradle) | Include Spring Boot plugin in build.gradle. |
| IDE configuration mistakes | Re-check run configurations and clean/rebuild the project. |
Conclusion
Encountering the "Cannot find main class" error can be daunting, especially for beginners learning about Spring Boot. By following the troubleshooting steps outlined above and understanding the potential root causes, developers can swiftly resolve this issue and ensure a smoother development process with Spring Boot applications. As a best practice, always review configurations whenever modifying the project structure or dependencies.
Related reading
- Spring Boot project in IntelliJ community edition
- Spring Boot project shows the Login page
- Spring Boot project with static content generates 404 when running jar
- Spring Boot properties in 'application.yml' not loading from JUnit Test
- Spring Boot PSQLException FATAL sorry, too many clients already when running tests
- Spring Boot Remove Whitelabel Error Page
- spring boot rabbitmq MappingJackson2MessageConverter custom object conversion
- Spring Boot read list from yaml using Value or ConfigurationProperties

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.