In this blog post, I will share 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
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.
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 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.
- Navigate to the directory that contains your Spring Boot application’s JAR file in the command line terminal.
- 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
javaspecifies that you want to run a Java program
-jartells Java to run the JAR file specified after it
-Xdebugtells Java to enable debugging
-Xrunjdwptells Java to use the Java Debug Wire Protocol (JDWP) for debugging
server=yspecifies that the JVM will act as the debug server
transport=dt_socketspecifies the communication protocol to use for debugging
address=8000specifies the port number to use for debugging
suspend=nspecifies whether the application should start suspended until a debugger is attached.
nmeans that it should not be suspended and should start immediately.
your-app.jaris the name of the JAR file containing your Spring Boot application.
- 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.
- Start the remote debugging session in your IDE.
- 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 "Run Spring Boot App from a Command Line"
Is there anyway to start it in debug mode for local testing purpose?