Three Ways to Start a Spring Boot Application

This article explains the three primary ways to launch a Spring Boot application—running a class with a main method, using the java -jar command, and leveraging the spring-boot-maven-plugin—along with code examples, configuration details, and Maven command options.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Three Ways to Start a Spring Boot Application

Spring Boot provides three main startup methods: executing a class that contains a main method, running the packaged JAR with java -jar, and using the spring-boot-maven-plugin.

1. Execute a class with a main method

This is the simplest approach, often done directly from an IDE like IntelliJ IDEA, which automatically loads configuration files from the classpath.

@RestController
@EnableAutoConfiguration
public class Example {
    @RequestMapping("/")
    public String home() {
        return "Hello World";
    }
    public static void main(String[] args) {
        /**
         * SpringApplication will automatically load <code>application.properties</code> from several locations:
         * 1. <b>/config</b> subdirectory of the current directory;
         * 2. The current directory;
         * 3. A classpath <code>/config</code> package;
         * 4. The classpath root.
         */
        SpringApplication.run(Example.class, args);
    }
}

In IDEA you can also set application parameters via the run configuration.

2. Use java -jar command

java -jar jar_path --param
jar_path

refers to the location of the packaged JAR file, and --param represents command‑line arguments, e.g.: java -jar example.jar --server.port=8081 This overrides the port defined in application.properties.

3. Start via spring-boot-maven-plugin

Add the following plugin configuration to your Maven project (the parent spring-boot-starter-parent already provides the appropriate version):

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <!-- version is inherited from the parent -->
</plugin>

After setting up, run the application from the project root: mvn spring-boot:run To pass arguments, consult the plugin’s help: mvn spring-boot:help -Ddetail Then specify arguments using -Drun.arguments, for example:

mvn spring-boot:run -Drun.arguments="--server.port=8888"

This command passes the specified parameters to the application at runtime.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavamavenSpring Bootstartup
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.