How to Run Spring Boot App via Command Line

In this blog post, I will share how to run a Spring Boot app from the command line in a Terminal window. There are a couple of ways to do this, and both assume that you have already created your Spring Boot app. If you need help creating a new Spring Boot app, please check out my previous blog post:

Create a Simple Web Service Project with Spring Boot

Add Maven Plugin to POM.XML

To run the Spring Boot application as a single executable Java jar file, we first need to update the pom.xml file of our project and add 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.

maven package

or you can also use

mvn 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 the Maven plugin to run your Spring Boot app. Use the below example to run your Spring Boot app with the Maven plugin:

mvn spring-boot:run

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:

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>

Run In Debug Mode

It is also possible to run your Spring Boot application in debug mode using command line terminal. This can be particularly useful when you need to debug your application on a machine without a graphical interface, or when you prefer to use the command line for debugging. In this scenario, the application will be launched with the necessary parameters to allow a remote debugger to connect to it, and then you can connect to it from an IDE or debugging tool. In the following steps, I will provide guidance on how to start your Spring Boot application in debug mode from a command line terminal.

  1. Navigate to the directory that contains your Spring Boot application’s JAR file in the command line terminal.
  2. Enter the following command to start your application in debug mode:
java -jar -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n your-app.jar

Where:

  • java specifies that you want to run a Java program
  • -jar tells Java to run the JAR file specified after it
  • -Xdebug tells Java to enable debugging
  • -Xrunjdwp tells Java to use the Java Debug Wire Protocol (JDWP) for debugging
  • server=y specifies that the JVM will act as the debug server
  • transport=dt_socket specifies the communication protocol to use for debugging
  • address=8000 specifies the port number to use for debugging
  • suspend=n specifies whether the application should start suspended until a debugger is attached. n means that it should not be suspended and should start immediately.
  • your-app.jar is the name of the JAR file containing your Spring Boot application.
  1. Open your IDE and set up a remote debugging session. In IntelliJ IDEA, for example, you can do this by selecting “Edit Configurations” from the Run menu, clicking the “+” button, selecting “Remote” from the list, and setting the “Debugger mode” to “Attach”. Then set the host and port to the same values you specified in the command line.
  2. Start the remote debugging session in your IDE.
  3. Your Spring Boot application should now be running in debug mode and be ready to receive debugging requests from your IDE.

Run Spring Boot Web Service as a Standalone App. Video Tutorial.


1 Comment on "How to Run Spring Boot App via Command Line"

Leave a Reply

Your email address will not be published. Required fields are marked *