How do I tell Spring Boot which main class to use for the executable jar?

JavaSpringMavenExecutable JarSpring Boot

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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 - Java

For those using Gradle (instead of Maven) :

springBoot {
    mainClass = "com.example.Main"
}

Solution 3 - Java

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 - Java

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 - Java

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.

See this Spring docs page.

A very important aspect here is to mention that the directory structure has to be src/main/java/nameofyourpackage

Solution 6 - Java

I tried the following code in pom.xml and it worked for me

<build>
<plugins>
	<plugin>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-maven-plugin</artifactId>
		<configuration>
			<mainClass>myPackage.HelloWorld</mainClass>	
		</configuration>
	</plugin>
	<plugin>
		<artifactId>maven-compiler-plugin</artifactId>
		<configuration>
			<fork>true</fork>
			<executable>D:\jdk1.8\bin\javaw.exe</executable>
		</configuration>
	</plugin>
</plugins>

Solution 7 - Java

Since Spring Boot 1.5, you can complete ignore the error-prone string literal in pom or build.gradle. The repackaging tool (via maven or gradle plugin) will pick the one annotated with @SpringBootApplication for you. (Refer to this issue for detail: https://github.com/spring-projects/spring-boot/issues/6496 )

Solution 8 - Java

I had renamed my project and it was still finding the old Application class on the build path. I removed it in the 'build' folder and all was fine.

Solution 9 - Java

Have seen this issue with Java 1.9 and SpringBoot 1.5.x, when main-class is not specified explicitly.

With Java 1.8, it is able to find main-class without explicit property and 'mvn package' works fine.

Solution 10 - Java

If you are using Grade, it is possible to apply the 'application' plugin rather than the 'java' plugin. This allows specifying the main class as below without using any Spring Boot Gradle plugin tasks.

plugins {
  id 'org.springframework.boot' version '2.3.3.RELEASE'
  id 'io.spring.dependency-management' version '1.0.10.RELEASE'
  id 'application'
}
application {
  mainClassName = 'com.example.ExampleApplication'
}

As a nice benefit, one is able to run the application using gradle run with the classpath automatically configured by Gradle. The plugin also packages the application as a TAR and/or ZIP including operating system specific start scripts.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThiloView Question on Stackoverflow
Solution 1 - Javaludo_rjView Answer on Stackoverflow
Solution 2 - JavaMarcelo C.View Answer on Stackoverflow
Solution 3 - JavaRenaudView Answer on Stackoverflow
Solution 4 - JavaShane LuView Answer on Stackoverflow
Solution 5 - JavamojaveView Answer on Stackoverflow
Solution 6 - JavaTRIDIB BOSEView Answer on Stackoverflow
Solution 7 - Javatan9View Answer on Stackoverflow
Solution 8 - JavaAnthony De SmetView Answer on Stackoverflow
Solution 9 - Javady10View Answer on Stackoverflow
Solution 10 - JavadbaltorView Answer on Stackoverflow