Run Spring Boot App from a Command Line

With this blog post, I am going to share with you how to run a Spring Boot app from a command line in a Terminal window. There are a couple of ways and both assume that you have already created your Spring Boot app. To learn how to create a new Spring Boot app please check the below blog post of mine.

Create a Simple Web Service Project with Spring Boot

Build Spring Boot Project with Maven

To be able to run your Spring Boot app you will need to first build it. To build a Spring Boot app with a Maven use the below command. You will need to run it from the project folder which contains the pom.xml file.

maven install

Run Spring Boot app with java -jar command

To run your Spring Boot app from a command line in a Terminal window you can you the java -jar command. This is provided your Spring Boot app was packaged as an executable jar file.

java -jar target/mywebserviceapp-0.0.1-SNAPSHOT.jar

Run Spring Boot app using Maven

You can also use Maven plugin to run your Spring Boot app. Use the below example to run your Spring Boot app with Maven plugin:

mvn spring-boot:run

Run Spring Boot App with Gradle

And if you use Gradle you can run the Spring Boot app with the following command:

gradle bootRun

Automatic Restart and Hot Swapping

Applications that use spring-boot-devtools dependency automatically restart whenever files on the classpath change. Although for this to work you will need to run your Spring Boot App using IDE.

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-devtools</artifactId>
 <optional>true</optional>
</dependency>