DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Spring Boot Secured By Let's Encrypt

Trending

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • A Guide to Container Runtimes
  • Solid Testing Strategies for Salesforce Releases
  • Internal Developer Portals: Modern DevOps's Missing Piece
  1. DZone
  2. Coding
  3. Frameworks
  4. Configuring a Main Class in Spring Boot

Configuring a Main Class in Spring Boot

In this article, see a tutorial that explains how to configure a main class in Spring Boot.

By 
Shabbir Dawoodi user avatar
Shabbir Dawoodi
·
Updated Mar. 11, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
84.3K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Java
 


xxxxxxxxxx
1
 
1
@SpringBootApplication
2
public class SpringBootMainClassApplication {
3
 
4
    public static void main(String[] args) {
5
        SpringApplication.run(SpringBootMainClassApplication.class, args);
6
    }
7
 
8
}


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

Java
 


xxxxxxxxxx
1
10
 
1
@Controller
2
public class HomeController {
3
 
4
    @RequestMapping("/")
5
    @ResponseBody
6
    public String goToHomePage () {
7
        return "<h1>This is the Home page</h1>";
8
    }
9
 
10
}


You may also be interested in: Hibernate 5 Java Configuration Example

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

XML
x
43
 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4
    <modelVersion>4.0.0</modelVersion>
5
    <parent>
6
        <groupId>org.springframework.boot</groupId>
7
        <artifactId>spring-boot-starter-parent</artifactId>
8
        <version>2.1.9.RELEASE</version>
9
        <relativePath></relativePath> <!-- lookup parent from repository -->
10
    </parent>
11
    <groupId>com.dailycodebufffer.example</groupId>
12
    <artifactId>Spring-Boot-Main-Class</artifactId>
13
    <version>0.0.1-SNAPSHOT</version>
14
    <name>Spring-Boot-Main-Class</name>
15
    <description>Demo project for Spring Boot</description>
16
 
17
    <properties>
18
        <java.version>11</java.version>
19
    </properties>
20
 
21
    <dependencies>
22
        <dependency>
23
            <groupId>org.springframework.boot</groupId>
24
            <artifactId>spring-boot-starter-web</artifactId>
25
        </dependency>
26
 
27
        <dependency>
28
            <groupId>org.springframework.boot</groupId>
29
            <artifactId>spring-boot-starter-test</artifactId>
30
            <scope>test</scope>
31
        </dependency>
32
    </dependencies>
33
 
34
    <build>
35
        <plugins>
36
            <plugin>
37
                <groupId>org.springframework.boot</groupId>
38
                <artifactId>spring-boot-maven-plugin</artifactId>
39
            </plugin>
40
        </plugins>
41
    </build>
42
 
43
</project>


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

Build success


Home page of the application

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

Java
xxxxxxxxxx
1
 
1
@SpringBootApplication
2
public class SpringBootMainClassApplication2 {
3
 
4
    public static void main(String[] args) {
5
        SpringApplication.run(SpringBootMainClassApplication2.class, args);
6
    }
7
}


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

Java application throws exception


How Do We 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:

XML
xxxxxxxxxx
1
11
 
1
<build>
2
        <plugins>
3
            <plugin>
4
                <groupId>org.springframework.boot</groupId>
5
                <artifactId>spring-boot-maven-plugin</artifactId>
6
                <configuration>
7
                    <mainClass>com.dailycodebufffer.example.SpringBootMainClass.SpringBootMainClassApplication</mainClass>
8
                </configuration>
9
            </plugin>
10
        </plugins>
11
    </build>


Voila! We can define any of the main classes in the configuration. Below, check out the successful build of our Spring Boot application.

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:

PowerShell
 


xxxxxxxxxx
1
 
1
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.

As always, you can find the code on GitHub.

Further Reading

The @SpringBootApplication Annotation Example in Java + Spring Boot

Spring Boot Configuration: Overriding Built-In Configuration

Spring Framework Spring Boot application

Opinions expressed by DZone contributors are their own.

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Spring Boot Secured By Let's Encrypt

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!