How do you run a Spring Boot application from the command line?

Table of Contents

Introduction

Running a Spring Boot application from the command line is a common and efficient way to start and test your application. Spring Boot provides several methods to run the application, whether you're using build tools like Maven or Gradle, or simply running an executable JAR file. This flexibility allows developers to easily deploy and test Spring Boot applications in different environments.

This guide will walk you through the steps for running a Spring Boot application from the command line using Maven, Gradle, and directly using an executable JAR file.

Methods to Run a Spring Boot Application from the Command Line

1. Running Spring Boot with Maven

If your project is managed with Maven, you can use the Spring Boot Maven Plugin to run your application directly from the command line. The plugin automates several tasks, including packaging and running the application.

Step 1: Ensure Maven is Installed

First, ensure that you have Maven installed on your system. You can check if Maven is installed by running:

If Maven is not installed, you can follow the installation instructions.

Step 2: Run the Application with Maven

Once your Spring Boot application is ready, navigate to your project directory where the pom.xml file is located. Then, use the following Maven command to run the application:

This command:

  • Compiles your application.
  • Runs the application in the same process.
  • Starts the embedded server (e.g., Tomcat) and makes your application accessible at the default port (e.g., http://localhost:8080).

Step 3: (Optional) Run with Specific Profiles

If you want to run the application with specific profiles (e.g., development, production), you can pass the -Dspring.profiles.active option like this:

This will use the application-dev.properties or application-dev.yml configuration file.

2. Running Spring Boot with Gradle

If you're using Gradle as your build tool, you can run the Spring Boot application using the Spring Boot Gradle Plugin.

Step 1: Ensure Gradle is Installed

Make sure Gradle is installed on your system. You can check if Gradle is installed by running:

If Gradle is not installed, you can follow the installation instructions.

Step 2: Run the Application with Gradle

Navigate to the root of your Gradle project (where the build.gradle file is located) and run the following command:

This command:

  • Compiles the application.
  • Runs the application directly from Gradle, just like the Maven approach.
  • Starts the embedded server and makes your application accessible.

Step 3: (Optional) Run with Specific Profiles

Similar to Maven, Gradle allows you to specify different Spring profiles. You can run the application with a specific profile by using the following command:

This will use the application-dev.properties or application-dev.yml configuration file.

3. Running a Spring Boot Executable JAR File

One of the most common and recommended ways to run a Spring Boot application is by creating an executable JAR file. This allows you to run the application anywhere with a Java runtime.

Step 1: Build the Executable JAR

First, ensure that your application is packaged as an executable JAR file. If you're using Maven, you can package your application by running:

This will create a JAR file in the target/ directory, usually named your-application-name-<version>.jar.

If you're using Gradle, run:

The resulting JAR file will be in the build/libs/ directory.

Step 2: Run the Executable JAR

Once you have the JAR file, you can run the Spring Boot application from the command line by using the java -jar command:

or if using Gradle:

This command will:

  • Start the Spring Boot application.
  • Run the embedded web server (e.g., Tomcat) and your application will be available at http://localhost:8080 by default.

Step 3: (Optional) Run with Specific Profiles

You can also specify Spring profiles while running the JAR file by using the --spring.profiles.active parameter:

This allows you to customize the application’s configuration based on the environment.

Running a Spring Boot Application in Production

For production environments, it's common to package the application as an executable JAR and run it directly on the server. This approach is lightweight and efficient. Additionally, running Spring Boot in the background (detached mode) can be achieved using tools like systemd, nohup, or Docker.

Example: Running in the Background with nohup:

This command runs the Spring Boot application in the background, allowing you to log the output to a file (output.log) and keep the process running even after you log out.

Conclusion

Running a Spring Boot application from the command line is straightforward and flexible, with multiple options depending on your build tool and deployment strategy. You can:

  • Use Maven with the spring-boot:run command to run the application directly from the build tool.
  • Use Gradle with the bootRun task for similar functionality.
  • Package your Spring Boot application as an executable JAR and run it using the java -jar command.

Each approach provides a simple and effective way to start your application in development or production environments. Whether you choose Maven, Gradle, or an executable JAR, the command line gives you the control to quickly deploy and test your Spring Boot applications.

Similar Questions