Run Spring Boot Application from a Command Line

In this article, you will learn how to run the Spring boot application using the command line in windows.

Quick Steps

You can quickly use the below commands to run a spring boot application from a command line.

1. Run the Spring boot application with the java -jar command:
$ java -jar target/myapplication-0.0.1-SNAPSHOT.jar
2. Run the Spring boot application using Maven:
$ mvn spring-boot:run
3. Run your Spring Boot application using Gradle
$ gradle bootRun

1. Run the Spring Boot app with the 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, build your 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 the Spring Boot App with Maven Plugin

Note on Spring Boot Maven plugin

If you are using maven as a project management and build tool then make sure that you have added the 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.

Maven Command to Run Spring Boot App

If you have installed Maven on your machine, then use the following Maven command to run a Spring Boot application:
$ mvn spring-boot:run
If you haven't installed Maven on your Machine, then use the following Maven command to run the Spring Boot application:
$ ./mvnw spring-boot:run

3. Run the Spring Boot App with Gradle

The Spring Boot Gradle plugin also includes a bootRun task that can be used to run your application in an exploded form. 
If you use Gradle, then use the following command to run your Spring Boot application:
$ gradle bootRun

Conclusion

In this article, you have seen how to run the Spring boot application using the command line in windows.

Comments