SpringBoot
Java
main class
troubleshooting
programming error

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.

Browse interview questions

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:

  1. 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.
  2. Manifest Misconfiguration: Sometimes this issue can arise if the MANIFEST.MF file in your JAR does not have the Main-Class attribute correctly specified.
  3. 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:

java
1@SpringBootApplication
2public class MySpringBootApplication {
3
4    public static void main(String[] args) {
5        SpringApplication.run(MySpringBootApplication.class, args);
6    }
7}

Scenario with Multiple Main Classes

Consider a scenario where multiple Java files contain a main method:

java
1@SpringBootApplication
2public class FirstMainApp {
3
4    public static void main(String[] args) {
5        SpringApplication.run(FirstMainApp.class, args);
6    }
7}
8
9public class SecondMainApp {
10
11    public static void main(String[] args) {
12        SpringApplication.run(SecondMainApp.class, args);
13    }
14}

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:

xml
<properties>
    <start-class>com.example.FirstMainApp</start-class>
</properties>

If you are using Gradle, add the following in your build.gradle:

groovy
springBoot {
    mainClass = 'com.example.FirstMainApp'
}

Correcting the Manifest File

Check and correct your JAR’s MANIFEST.MF file to specify the main class:

 
Manifest-Version: 1.0
Main-Class: com.example.FirstMainApp

Preventive Measures

  1. Maintain One Main Class: To avoid ambiguity, maintain only one main application class in a given module.
  2. Configure Correct Builds: Ensure your build configuration files (pom.xml or build.gradle) are correctly set up to declare the main class.
  3. Use Spring Boot Annotations: Use the @SpringBootApplication annotation 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

CauseSolution
Multiple main classesSpecify the main class explicitly using pom.xml (Maven) or build.gradle (Gradle)
Manifest misconfigurationSet the Main-Class in the MANIFEST.MF file
Invalid project structureEnsure 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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.