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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance
  • Going All In On the JVM 25 Virtual Threads
  • Java 8 Optional - Avoid Null and NullPointerException Altogether - and Keep It Pretty
  • Who Should Grade Your AI? Rules vs LLM Judges vs Humans

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Updated Mar. 11, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
85.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook