Spring Boot Error – Unable to Find a Single Main Class

1. Root Cause

If Spring boot application has multiple classes with –

  • main() method, or
  • @SpringBootApplication annotation

Then while running the application as jar, we can face the error “Unable to find a single main class”.

[java title=”Console”]
$ mvn spring-boot:run

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.1.RELEASE:run
(default-cli) on project main-class: Execution default-cli of goal
org.springframework.boot:spring-boot-maven-plugin:2.0.1.RELEASE:run failed:
Unable to find a single main class from the following candidates
[com.springexamples.demo.MainClassTwo, com.springexamples.demo.MainClassOne]
-> [Help 1]
[/java]

2. Solution

To solve the above problem, we need to configure the start-class property in pom.xml in properties section.

[xml title=”pom.xml” highlight=”5″]
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!– The main class to start –>
<start-class>com.springexamples.demo.MainClassOne</start-class>
</properties>
[/xml]

Now when we run the application again, we will not get this error and the application will be started with MainClassOne as spring boot application starter class for the given configuration.

Happy Learning !!

Leave a Comment