spring-boot
command-line
application-startup
java
development

How to start up spring-boot application via command line?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Spring Boot is a popular framework for building stand-alone, production-grade Spring-based applications. One of the advantages of a Spring Boot application is its ability to be launched from the command line, making deployment and integration with CI/CD pipelines much more manageable. This article provides a step-by-step guide on starting a Spring Boot application using the command line, including some technical explanations and illustrative examples.

Prerequisites

Before proceeding, ensure you have the following installed and set up on your system:

  • JDK (Java Development Kit): Spring Boot applications require Java to run. Download the latest version from the Oracle website or use a package manager suitable for your operating system.
  • Maven or Gradle (Optional): If you are building your Spring Boot application from source, you'll need either Maven or Gradle. You can skip this if you have a pre-built JAR file.
  • Spring Boot CLI (Optional): This is optional but can be beneficial for some tasks. More information is available on the Spring Boot CLI documentation.

Starting a Spring Boot Application

Step 1: Build or Obtain the Executable JAR

The initial step in running a Spring Boot application from the command line is to have its executable JAR file. If you're developing the application from scratch, use Maven or Gradle to create this file.

Using Maven

Navigate to your project directory and execute the following command:

bash
mvn clean install

This command compiles the Java code, runs unit tests, and packages the application into a JAR file under the target directory.

Using Gradle

For those who prefer Gradle, execute:

bash
gradle build

This will create the JAR file in the build/libs directory.

Note: If you are using Spring Boot's Maven or Gradle plugins, ensure you have the spring-boot-maven-plugin or org.springframework.boot plugin configured, respectively, to generate executable JARs.

Step 2: Run the Application

Once you have the JAR file, you can run it using the java -jar command followed by the JAR filename. For example:

bash
java -jar my-spring-boot-app.jar

Customizing the Application Run

There are several ways to customize how your Spring Boot application runs via command line arguments:

Using Application Properties

You can pass specific runtime configuration options using the -- syntax. For instance, to change the server's port:

bash
java -jar my-spring-boot-app.jar --server.port=8081

Setting Environment Variables

Environment variables can also be used to pass configurations:

bash
export SERVER_PORT=8081
java -jar my-spring-boot-app.jar

JVM Options

You might want to adjust Java Virtual Machine (JVM) parameters for performance tuning, such as heap size:

bash
java -Xmx1024m -jar my-spring-boot-app.jar

Additional Features and Considerations

  • Profile-Specific Properties: Use profiles to separate different environments (development, testing, production). Start the application with a specific profile using:
bash
  java -jar my-spring-boot-app.jar --spring.profiles.active=dev
  • Debugging: Enable debugging if required:
bash
  java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -jar my-spring-boot-app.jar
  • Logging: Spring Boot logs can be redirected or configured using logback or log4j2 configurations for more control over log outputs.

Summary Table

AspectDescription
JAR BuildingUse mvn clean install or gradle build to build the executable JAR.
Basic ExecutionRun the JAR with java -jar command.
Port ConfigurationAdjust port with --server.port argument.
Environment VariablesPass configurations using environment variables.
JVM ConfigurationUse -Xmx, -Xms for JVM parameters.
Active ProfilesSet profiles with --spring.profiles.active.
DebuggingEnable debugging options for remote checking of issues.

In conclusion, starting a Spring Boot application from the command line is straightforward, but it offers numerous customization options to suit varying environments and requirements. By understanding these options, developers can ensure their Spring Boot applications run efficiently and correctly in any situation.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.