Configuring a Main Class in Spring Boot

Spread the love

SpringApplication.run(Classname.class, args) bootstraps a spring application as a stand-alone application from the main method. It creates an appropriate ApplicationContext instance and load beans.

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.

Let’s see how we can Configure a Main Class in Spring Boot

@SpringBootApplication
public class SpringBootMainClassApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootMainClassApplication.class, args);
	}

}

This is the main class of our application form which Spring Boot application will get executed. Now Let’s create one Homepage controller to open the Home page of the application.

@Controller
public class HomeController {

    @RequestMapping("/")
    @ResponseBody
    public String goToHomePage () {
        return "<h1>This is the Home page</h1>";
    }

}

Below is the POM.xml file for out Spring Boot project.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.9.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.dailycodebufffer.example</groupId>
	<artifactId>Spring-Boot-Main-Class</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Spring-Boot-Main-Class</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>11</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Now, when you run the application with mvn spring-boot:run command in Terminal, the Spring Boot application will get started on port 8081 as configured in application.properties file. Check out the below screenshot for the Build Success and Home page of the application.

Spring Boot server success
Spring Boot application Home page

Now, Let’s create a new Spring Main class in the application. After the creation of this class, we will have two new main class with two public static void main(String args[]) methods.

@SpringBootApplication
public class SpringBootMainClassApplication2 {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMainClassApplication2.class, args);
    }
}

As we know from Java 101, we can only have one main method in Java application. When we will try to build this application, it will throw an exception as below.

Now, How to Solve this?

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.

Below are the changes required in the POM.xml file (highlighted changes)

<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<mainClass>com.dailycodebufffer.example.SpringBootMainClass.SpringBootMainClassApplication</mainClass>
				</configuration>
			</plugin>
		</plugins>
	</build>

voila!!! We can define any of the main classes in the configuration. Check below the successful build of our Spring Boot application.

Using CLI to configure the main class in Spring Boot

We can also specify a main class via the command-line interface.

Spring Boot’s org.springframework.boot.loader.PropertiesLauncher comes with a JVM argument to let you override the logical main-class called loader.main:

java -cp Spring-Boot-Main-Class-0.0.1-SNAPSHOT.jar -Dloader.main=com.dailycodebufffer.example.SpringBootMainClass.SpringBootMainClassApplication org.springframework.boot.loader.PropertiesLauncher


Conclusion

There are more than a few ways to specify the entry point to a Spring Boot application. It’s important to know that all these configurations are just different ways to modify the manifest of a JAR or WAR file.

Github Repository

As always, Please find the code in Github: Click Here