Run Spring Boot Application from a Command Line


In this short article, I show you how to run Spring boot application using the command line in windows.

Quick Steps

You can quickly refer below two ways to run spring boot application from a command line.
1. Run spring boot application with java -jar command:
$ java -jar target/myapplication-0.0.1-SNAPSHOT.jar
2. Run spring boot application with mvn spring-boot:run maven plugin:
$ mvn spring-boot:run

1. Run Spring Boot app with java -jar command

If you use the Spring Boot Maven or Gradle plugins to create an executable jar, you can run your application using java -jar. For example, let's build this maven project using mvn clean install and change the directory to the current project directory and run the following command in cmd.
$ java -jar target/myapplication-0.0.1-SNAPSHOT.jar
It is also possible to run a packaged application with remote debugging support enabled. Doing so lets you attach a debugger to your packaged application, as shown in the following example:
$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
       -jar target/myapplication-0.0.1-SNAPSHOT.jar

2. Run Spring Boot app with maven plugin (mvn spring-boot:run) command

Note on Spring Boot Maven plugin

If you are using maven as project management and build tool then make sure that you have added spring-boot-maven-plugin to your pom.xml file.

The Spring Boot Maven plugin provides many convenient features:
  • It collects all the jars on the classpath and builds a single, runnable "├╝ber-jar", which makes it more convenient to execute and transport your service.
  • It searches for the public static void main() method to flag as a runnable class.
  • It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.
<build>
        <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
     </build>

The Spring Boot Maven plugin includes a run goal that can be used to quickly compile and run your application. Applications run in an exploded form, as they do in your IDE. The following example shows a typical Maven command to run a Spring Boot application:
$ mvn spring-boot:run
We can also use the MAVEN_OPTS operating system environment variable, as shown in the following example:
$ export MAVEN_OPTS=-Xmx1024m

Five ways we can run Spring Boot Application

  1. Running from an IDE
  2. Running as a Packaged Application
  3. Using the Maven Plugin
  4. Using External Tomcat
  5. Using the Gradle Plugin

Comments