A Spring Boot application's main class is a class that contains a public static void main() method that starts up the Spring ApplicationContext. By default, if the main class isn't explicitly specified, Spring will search for one in the classpath at compile time and fail to start if none or multiple of them are found.
The Spring Boot Maven Pluginimplement a custom ClassLoader to locate and load all the external jar libraries now nested inside the package. automatically find the main() method and configure it in the manifest, so we don't have to specify the main class in our java -jar command.
Spring Boot allows us to define the Main class in the configuration when we have multiple main classes declared in the application. As we are using a MAVEN build, we have to configure the POM. xml for Spring Boot to identify the main class of the application.
By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath.
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>
For those using Gradle (instead of Maven) :
springBoot {
mainClass = "com.example.Main"
}
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>
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'
}
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With