How do I tell Spring Boot which main class to use for the executable jar?
Solution 1
Add your start class in your pom:
<properties>
<!-- The main class to start by executing java -jar -->
<start-class>com.mycorp.starter.HelloWorldApplication</start-class>
</properties>
or
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.mycorp.starter.HelloWorldApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
Solution 2
For those using Gradle (instead of Maven) :
springBoot {
mainClass = "com.example.Main"
}
Solution 3
If you do NOT use the spring-boot-starter-parent pom, then from the Spring documentation:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.1.3.RELEASE</version>
<configuration>
<mainClass>my.package.MyStartClass</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
Solution 4
For those using Gradle (instead of Maven), referencing here:
The main class can also be configured explicitly using the task’s mainClassName property:
bootJar {
mainClass = 'com.example.ExampleApplication'
}
Alternatively, the main class name can be configured project-wide using the mainClassName property of the Spring Boot DSL:
springBoot {
mainClass = 'com.example.ExampleApplication'
}
Solution 5
If you're using spring-boot-starter-parent in your pom, you simply add the following to your pom:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Then do your mvn package.
A very important aspect here is to mention that the directory structure has to be src/main/java/nameofyourpackage
Related videos on Youtube
hansvb
Updated on June 02, 2021Comments
-
hansvb almost 3 years
Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.0.1.RELEASE:repackage failed: Unable to find a single main class from the following candidates
My project has more than one class with a
main
method. How do I tell the Spring Boot Maven plugin which of the classes it should use as the main class?-
Evgeni Dimitrov about 10 yearsjava -cp myjar.jar MyClass
-
hansvb about 10 years@Evgeni: That's a runtime flag. It doesn't get that far. It fails to build.
-
-
Dave Syer about 10 yearsNo need to disable anything. Or am I missing something?
-
Dave Syer about 10 yearsNote that this answer is correct if you use the spring-boot-starter-parent pom. In that case the "start-class" property is applied to the "mainClass" configuration parameter of the spring-boot-maven-plugin (which you can do directly if you aren't using the starter).
-
Admin about 8 yearsI found this solution to work without modifying the pom.xml once I replicated the package requirements for the .java classes.
-
zhuguowei almost 8 yearsThanks @ludo_rj, and I found this also works:
mvn clean package -Dstart-class=com.foo.Application
, if want to dynamically specify using which main class -
Cheloute over 6 yearsI tried to use the spring-boot-maven-plugin configuration you mentionned in my multimodules maven project, composed by several Spring boot projects and including Spring Boot as BOM dependency, and it worked like a charm. Concerning the maven-compiler-plugin, I didn't specify anything as I don't want my POM platform dependent. Maven automatically forked, so I think you can just ignore this configuration.
-
Thunderforge almost 6 yearsSpring Boot 2.x gives an error
Could not set unknown property 'mainClass' for object of type org.springframework.boot.gradle.dsl.SpringBootExtension
. -
Vitalik almost 6 yearsThe answer is down this page: stackoverflow.com/a/49716696/75672
-
Gerardo Roza over 5 yearsOne more thing to add, the parameter mentioned by @zhuguowei is also valid for the Spring Boot Maven Plugin:
mvn spring-boot:run -Dstart-class=com.foo.Application
. This is valid only if you haven't specified the mainClass in the pom's plugin -
zhaozhi about 5 yearsmain class config can be omitted
-
Renaud about 5 years@zhaozhi could you explain why / how?
-
Angel O'Sphere over 4 yearsBoth did not work for me. I also thought it was an "AND" not an or? I see the Start-Class: correctly in the MANIFEST.MF, but spring starts a different annotated @SpringBootApplication main class. I actually need that class for bootstrapping some things so I do not really like to change the annotation. Simply removing it did not work anyway. Spring seems to start the first main() it finds. I'm using spring-boot-starter-parent 2.2.0.M3.