1. Overview
This article is going to cover shortly how to run a Spring Boot application. This could be useful for those who’s just started with Spring Boot.
Spring Boot application is often packaged in a jar file and embedded an HTTP server inside. Therefore, we don’t need to deploy and run the applications in a Servlet container as traditional way. There are several ways to run a Spring Boot applications:
- Run from an IDE
- Run as a packaged application
- Using Maven plugin
- Using Gradle plugin
We will cover one by one in next section.
2. Run a Spring Boot application from an IDE
Let’s take a look at an entry point of a sample Spring Boot application:
1 2 3 4 5 6 |
@SpringBootApplication public class HelloSpringBootWebApp { public static void main(String[] args) { SpringApplication.run(HelloSpringBootWebApp.class, args); } } |
The class contains a main method like a normal Java application. Therefore, we can run a Spring Boot application as a simple Java application. However, before running from an IDE, we must import the application into the IDE first.
To run a Spring Boot application with Eclipse:
- Open the entry point class which is the class that has the main method and @SpringBootApplication annotation.
- Right Click –> Run As –> Java Application or use the shortcut: Alt-Shift-X, J
And to run a Spring Boot application with IntelliJ:
- Open the entry point class which is the class that has the main method and @SpringBootApplication annotation.
- Right Click –> Run ‘Application.main()‘ or use the shortcut: Ctrl-Shift-F10
3. Run a Spring Boot application as a packaged application
If we want to run a Spring Boot application independently, we can package it into an executable jar by using Maven or Gradle. After creating the jar file, we can run the application by using the java -jar command. For example:
1 |
java -jar target/hello-springboot-app-0.0.1-SNAPSHOT.jar |
4. Run a Spring Boot application using Maven plugin
We can use spring-boot-maven-plugin to compile and run Spring Boot applications using Maven. To verify whether the plugin is included in the POM file or not, we can check the file for:
1 2 3 4 5 6 7 8 9 |
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.4.1.RELEASE</version> </plugin> </plugins> </build> |
To run a Spring Boot application using Maven, we simply need to execute:
1 |
mvn spring-boot:run |
5. Run a Spring Boot application using the Gradle plugin
In similar to the spring-boot-maven-plugin, Spring Boot provides Gradle users the plugin called spring-boot-gradle-plugin which we can use to quickly compile and run Spring Boot applications using Gradle.
To verify whether the plugin is included in the build.gradle file or not, we can check for:
1 2 3 4 5 6 |
buildscript { dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.1.RELEASE") } } apply plugin: 'spring-boot' |
And to run a Spring Boot application with Gradle, we can issue the command:
1 |
gradle bootRun |
6. Conclusion
We have learned about how to run a Spring Boot application. There are several ways to support us in doing that. And we can select the most suitable way for us. If we’re in development, we can run the application with IDE. However, when we want to deploy the application for other purposes, we have to package the application and run it using Gradle, Maven or java -jar command.
In addition, below are other tutorials related to Spring Boot, you can take them as a reference if needed:
Spring Boot – How To Set Active Profiles
Install Spring Boot Command Line Interface on Linux
Install Spring Boot Command Line Interface on Windows
How to Configure Logging in Spring Boot
Package a Spring Boot Application Into War File