Complete Guide to Starting Spring Boot Applications via Command Line

Nov 12, 2025 · Programming · 12 views · 7.8

Keywords: Spring Boot | Command Line Startup | Java Application Deployment | Maven | Gradle | Executable JAR

Abstract: This article provides a comprehensive guide to starting Spring Boot applications through command line, focusing on the correct usage of java -jar command for direct JAR execution. It covers startup commands for both Maven and Gradle build tools, analyzes Spring Boot application structure and auto-configuration mechanisms, and offers solutions for common startup errors, providing developers with complete command-line deployment guidance.

Overview of Spring Boot Command Line Startup

Spring Boot framework significantly simplifies Java application deployment and execution through its unique auto-configuration mechanism and embedded server support. In practical development environments, developers often need to start Spring Boot applications via command line, especially in production deployment or continuous integration scenarios. This article begins with application structure analysis and progressively explains various command line startup methods.

Spring Boot Application Structure Analysis

A standard Spring Boot application typically includes a main class annotated with @SpringBootApplication, which extends SpringBootServletInitializer to support traditional WAR deployment. As shown in the example code:

@SpringBootApplication(scanBasePackages = {"com.ubs.tas.topcat.dashboard"})
public class Application extends SpringBootServletInitializer {
    private static final Logger LOGGER = LoggerFactory.getLogger(Application.class.getName());
    private static final Class<Application> applicationClass = Application.class;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(applicationClass);
    }

    public static void main(String[] args) {
        LOGGER.info("Starting...");
        SpringApplication.run(Application.class, args);
    }
}

This annotation combines the functionality of @Configuration, @EnableAutoConfiguration, and @ComponentScan, implementing auto-configuration based on classpath and component scanning.

JAR File Building and Direct Execution

The most recommended command line startup method is building an executable JAR file and running it directly. First, use build tools to generate a complete JAR package containing all dependencies:

# Using Maven build
mvn clean package

# Using Gradle build
gradle build

After building, an executable JAR file will be generated in the target or build/libs directory. The complete syntax for the run command is:

java -jar path/to/your/jarfile.jar fully.qualified.package.Application

The advantage of this approach is that the JAR file contains all application dependencies and embedded servers, achieving true "one-click deployment". Spring Boot's loader module ensures correct dependency loading and execution.

Direct Execution via Build Tools

For development environments, you can use the quick commands provided by build tools to run applications directly:

# Maven approach
mvn spring-boot:run

# Gradle approach
./gradlew bootRun

These commands compile and run applications directly without generating standalone JAR files, suitable for rapid development and testing. Note that this approach requires build tools to be properly configured with environment variables.

Common Error Analysis and Solutions

Common errors during command line startup include:

Spring Boot Auto-configuration Mechanism

The core advantage of Spring Boot lies in its intelligent auto-configuration mechanism. The framework detects dependencies on the classpath and automatically configures corresponding Beans based on these dependencies. For example:

This mechanism ensures developers can focus on business logic without manually configuring infrastructure components.

Production Environment Deployment Recommendations

When deploying Spring Boot applications in production environments, it is recommended to:

Conclusion

Spring Boot provides multiple flexible command line startup methods, from direct execution via build tools during development to executable JAR deployment in production environments. Understanding the applicable scenarios and implementation principles of these methods can help developers deploy and maintain applications more efficiently. By mastering correct startup commands and configuration methods, developers can fully leverage Spring Boot's advantages in simplifying deployment.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.