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.
Add Maven Plugin to POM.XML
For us to be able to run Spring Boot application as a single executable Java jar file we first need to update the pom.xml file of our project and add to it a maven plugin.
Open the pom.xml file and add the following XML snippet below the list of project dependencies.
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
Build Spring Boot Project with Maven
To be able to run your Spring Boot app, you will need to first build it. To build and package a Spring Boot app into a single executable Jar file with a Maven, use the below command. You will need to run it from the project folder containing the pom.xml file.
or you can also use
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 the Maven plugin to run your Spring Boot app. Use the below example to run your Spring Boot app with the Maven plugin:
You can also run the above command with command-line arguments. To learn how to pass arguments to your Spring Boot application at runtime, read the following tutorial, “Pass command-line arguments to a Spring Boot application“.
Run Spring Boot App with Gradle
And if you use Gradle, you can run the Spring Boot app with the following command:
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>
Run Spring Boot Web Service as a Standalone App. Video Tutorial.