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.
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:
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:
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:
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:
Setting Environment Variables
Environment variables can also be used to pass configurations:
JVM Options
You might want to adjust Java Virtual Machine (JVM) parameters for performance tuning, such as heap size:
Additional Features and Considerations
- Profile-Specific Properties: Use profiles to separate different environments (development, testing, production). Start the application with a specific profile using:
- Debugging: Enable debugging if required:
- Logging: Spring Boot logs can be redirected or configured using
logbackorlog4j2configurations for more control over log outputs.
Summary Table
| Aspect | Description |
| JAR Building | Use mvn clean install
or gradle build to build the executable JAR. |
| Basic Execution | Run the JAR with java -jar command. |
| Port Configuration | Adjust port with --server.port argument. |
| Environment Variables | Pass configurations using environment variables. |
| JVM Configuration | Use -Xmx, -Xms for JVM parameters. |
| Active Profiles | Set profiles with --spring.profiles.active. |
| Debugging | Enable 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
- How to start up spring-boot application via command line?
- How to stop a thread created by implementing runnable interface?
- How to store printStackTrace into a string
- How To Stream Chunked Response With Spring Boot RestController
- How to subtract X day from a Date object in Java?
- How to subtract X days from a date using Java calendar?
- How to supply value to an annotation from a Constant java
- How to synchronize a static variable among threads running different instances of a class in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.