How to Run A Spring Boot Application From the Command Line?

17 minutes read

To run a Spring Boot application from the command line, you can follow these steps:

  1. Open your command-line interface (e.g., Command Prompt on Windows or Terminal on macOS/Linux).
  2. Navigate to the root directory of your Spring Boot application using the cd command. For example: cd /path/to/your/spring-boot-app
  3. Build your application using Maven or Gradle build tools. Use either of the following commands: If you're using Maven: mvn clean install If you're using Gradle: gradlew clean build This command compiles your application source code, resolves dependencies, and generates an executable JAR file.
  4. Once the build is complete, you can find the executable JAR file in the target directory (Maven) or build/libs directory (Gradle).
  5. To run the Spring Boot application, use the java -jar command followed by the name of the JAR file. For example: java -jar my-application.jar Replace my-application.jar with the actual name of your JAR file.
  6. The Spring Boot application will start and run. You can access it by opening a web browser and navigating to http://localhost:8080 (assuming the application runs on the default port).


Note: Make sure you have Java Development Kit (JDK) installed and properly configured on your system. Spring Boot requires JDK 8 or later versions.


That's it! You have successfully run your Spring Boot application from the command line.

Best Spring Boot Books to Read in December 2024

1
Full Stack Development with Spring Boot and React: Build modern and scalable web applications using the power of Java and React, 3rd Edition

Rating is 5 out of 5

Full Stack Development with Spring Boot and React: Build modern and scalable web applications using the power of Java and React, 3rd Edition

2
Spring Boot Persistence Best Practices: Optimize Java Persistence Performance in Spring Boot Applications

Rating is 4.9 out of 5

Spring Boot Persistence Best Practices: Optimize Java Persistence Performance in Spring Boot Applications

3
Spring Boot in Action

Rating is 4.8 out of 5

Spring Boot in Action

4
Spring Boot: Up and Running: Building Cloud Native Java and Kotlin Applications

Rating is 4.7 out of 5

Spring Boot: Up and Running: Building Cloud Native Java and Kotlin Applications

5
Learning Spring Boot 3.0: Simplify the development of production-grade applications using Java and Spring, 3rd Edition

Rating is 4.6 out of 5

Learning Spring Boot 3.0: Simplify the development of production-grade applications using Java and Spring, 3rd Edition

6
Spring in Action, Sixth Edition

Rating is 4.5 out of 5

Spring in Action, Sixth Edition

7
Modern API Development with Spring and Spring Boot: Design highly scalable and maintainable APIs with REST, gRPC, GraphQL, and the reactive paradigm

Rating is 4.4 out of 5

Modern API Development with Spring and Spring Boot: Design highly scalable and maintainable APIs with REST, gRPC, GraphQL, and the reactive paradigm

8
Spring Boot and Angular: Hands-on full stack web development with Java, Spring, and Angular

Rating is 4.3 out of 5

Spring Boot and Angular: Hands-on full stack web development with Java, Spring, and Angular


Can I run a Spring Boot application with custom JVM arguments?

Yes, you can run a Spring Boot application with custom JVM arguments.


To pass custom JVM arguments, you need to update the JAVA_OPTS environment variable while executing the Spring Boot application.


For example, if you are executing the application using the java -jar command, you can pass custom JVM arguments using the following syntax:

1
java -jar -D<key>=<value> <application-jar-name>.jar


Here, the -D<key>=<value> part is used to define the custom JVM argument. You can specify multiple arguments by separating them with spaces.


Alternatively, if you are using an IDE or a build tool to run the Spring Boot application, you can update the run configuration or build configuration to include the custom JVM arguments. The process may vary based on the IDE or build tool you are using.


Is there a way to customize the banner displayed when running a Spring Boot application?

Yes, there is a way to customize the banner displayed when running a Spring Boot application.


You can create a banner.txt or banner.gif file in the src/main/resources directory of your Spring Boot project. This file can contain ASCII art or custom image that you want to display as the banner.


Here is an example of a banner.txt file:

1
2
3
4
5
 _____                 _         ____  _               ___  
| ____|_   _____ _ __ | |_      / ___|| |__   ___  ___|__ \ 
|  _| \ \ / / _ \ '_ \| __|____| |   | '_ \ / _ \/ _ \ / / 
| |___ \ V /  __/ | | | ||_____| |___| | | |  __/ (_) |_|  
|_____| \_/ \___|_| |_|\__|     \____|_| |_|\___|\___/(_)


Spring Boot also provides additional customization options for the banner. You can set the banner file location in your application.properties or application.yml file using the spring.banner.location property. For example:


application.properties:

1
spring.banner.location=classpath:my-custom-banner.txt


application.yml:

1
2
3
spring:
  banner:
    location: classpath:my-custom-banner.txt


You can also disable the banner completely by setting the spring.main.banner-mode property to off in your application.properties or application.yml file:


application.properties:

1
spring.main.banner-mode=off


application.yml:

1
2
3
spring:
  main:
    banner-mode: off


These customization options allow you to personalize the banner displayed when running your Spring Boot application.


How do I pass command line arguments to a Spring Boot application?

To pass command line arguments to a Spring Boot application, you can use the following steps:

  1. Annotate your main method with @SpringBootApplication and implement the CommandLineRunner interface. The CommandLineRunner interface provides a run method that can be overridden to handle the command line arguments.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
@SpringBootApplication
public class MySpringBootApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        // Handle command line arguments here
    }
}


  1. In the run method, you can access the command line arguments using the args parameter. The args parameter is an array of strings that contain the command line arguments.
1
2
3
4
5
6
7
@Override
public void run(String... args) throws Exception {
    // Access command line arguments
    for (String arg : args) {
        System.out.println("Command line argument: " + arg);
    }
}


  1. Run the Spring Boot application from the command line and pass the desired arguments. For example, if you are using Maven to build and run the application, you can use the following command:
1
mvn spring-boot:run --args='arg1 arg2 arg3'


  1. The passed command line arguments will be available in the run method of your Spring Boot application and you can handle them as per your requirements.

Best Cloud Providers to Host Java Spring Boot in 2024

1
AWS

Rating is 5 out of 5

AWS

2
DigitalOcean

Rating is 4.9 out of 5

DigitalOcean

3
Vultr

Rating is 4.8 out of 5

Vultr

4
Cloudways

Rating is 4.7 out of 5

Cloudways


What are the commonly used Spring Boot command line options?

Some of the commonly used Spring Boot command line options are:

  1. --spring.profiles.active: Specifies the active profiles to be used.
  2. --server.port: Specifies the port number on which the application should listen.
  3. --spring.config.name: Specifies the name of the configuration file.
  4. --spring.config.location: Specifies the location of the configuration file.
  5. --debug: Enables debugging output for the application.
  6. --spring.output.ansi.enabled: Enables or disables ANSI output.
  7. --spring.main.banner-mode: Specifies the mode for displaying the application banner.
  8. --spring.main.sources: Specifies the sources to be used for the application context.
  9. --spring.main.web-application-type: Specifies the type of the web application.
  10. --spring.devtools.restart.enabled: Enables or disables automatic application restart.


These are just a few examples, and there are many more options available depending on the specific requirements of the Spring Boot application.


Is it possible to package a Spring Boot application as a WAR file?

Yes, it is possible to package a Spring Boot application as a WAR (Web Application Archive) file instead of the default JAR (Java Archive) file. This can be done by making a few modifications to the Spring Boot application.


Here are the steps to package a Spring Boot application as a WAR file:

  1. Modify the pom.xml file of the Spring Boot application to change the packaging type from jar to war. Open the pom.xml file and change the element to war.
  2. Update the main method in the application startup class. The main method should extend the SpringBootServletInitializer class and override the configure method. This is required to provide a deployment configuration for the Servlet container.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
@SpringBootApplication
public class YourApplication extends SpringBootServletInitializer {

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

  public static void main(String[] args) {
    SpringApplication.run(YourApplication.class, args);
  }

}


  1. Build the application using Maven or Gradle. For Maven, use the command mvn clean package and for Gradle, use gradle clean build.
  2. After the build is successful, the generated WAR file will be available in the target or build/libs directory, depending on the build system used.


The resulting WAR file can be deployed to a Servlet container such as Apache Tomcat or Jetty.


How can I check if Java is installed on my machine?

There are a couple of ways to check if Java is installed on your machine:

  1. Command Line: Open the Command Prompt (Windows) or Terminal (macOS/Linux) and type java -version. If Java is installed, it will display the version information. Otherwise, it will show an error indicating that Java is not recognized as a command.
  2. Control Panel (Windows): Go to the Control Panel, select "Programs" or "Programs and Features", and look for "Java" in the list of installed programs.
  3. System Preferences (macOS): Open System Preferences, click on "Java" (usually located in the Other section), and if Java is installed, it will show a dialog box with the version information.
  4. Java Control Panel: Open the Java Control Panel (can be found in the Windows Control Panel or macOS System Preferences) and go to the "Java" tab. If Java is installed, it will display the version information.


It's worth noting that on some systems, Java may exist in various forms, such as the Java Development Kit (JDK) or Java Runtime Environment (JRE). So, if you specifically need one of these versions, ensure that the correct version is installed.


How can I specify specific application properties when running a Spring Boot application?

You can specify specific application properties when running a Spring Boot application in several ways:

  1. Command Line Arguments: You can pass the properties as command-line arguments when starting the application. For example, java -jar myapp.jar --spring.datasource.url=jdbc:mysql://localhost:3306/mydb.
  2. Application Properties File: You can provide a properties file (e.g., application.properties or application.yml) in the classpath or the current directory. The application will automatically pick up the properties from the file.
  3. Externalized Configuration: You can use external configuration files like application.properties or application.yml located outside the application's classpath. These files can be placed in a predefined location on the file system or specified using the spring.config.name, spring.config.location, or spring.config.additional-location properties.
  4. Environment Variables: You can set the properties as environment variables, and the application will automatically pick them up. For example, SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/mydb.
  5. System Properties: You can pass the properties as system properties by using the -D flag during application startup. For example, java -jar myapp.jar -Dspring.datasource.url=jdbc:mysql://localhost:3306/mydb.
  6. Configuration Profiles: You can define application properties specific to different profiles like development, production, or testing. You can activate a specific profile using command-line arguments (--spring.profiles.active=profile) or by setting the SPRING_PROFILES_ACTIVE environment variable.


Note that the priority of property sources is determined by the order listed above, meaning properties specified with higher precedence override the ones specified with lower precedence.


How do I enable the production mode when running a Spring Boot application?

To enable the production mode in a Spring Boot application, you need to set the spring.profiles.active property to production. There are several ways to achieve this:

  1. In application.properties or application.yml: Add the following line to the configuration file. spring.profiles.active=production
  2. Command line argument: When running the application, pass the --spring.profiles.active=production argument. java -jar your-application.jar --spring.profiles.active=production
  3. Environment variable: Set the SPRING_PROFILES_ACTIVE environment variable to production. export SPRING_PROFILES_ACTIVE=production
  4. System property: Set the spring.profiles.active system property to production when starting your application. java -jar -Dspring.profiles.active=production your-application.jar


Choose the method that suits your deployment environment or workflow. After enabling the production mode, Spring Boot will load the corresponding configuration files or beans specific to the production profile.


What is the command to create a new Spring Boot application?

To create a new Spring Boot application, you can use the Spring Initializr. The Spring Initializr is a web-based tool that allows you to select the dependencies and configurations for your Spring Boot application and generates a project structure for you.


You can access the Spring Initializr at https://start.spring.io/.


To create a new Spring Boot application using the command line, you can use the curl command to send a GET request to the Spring Initializr with the desired project configurations. Here is an example command:

1
2
3
4
5
6
curl https://start.spring.io/starter.zip -o my-spring-app.zip \
-d dependencies=web,devtools \
-d language=java \
-d type=maven-project \
-d javaVersion=11 \
-d bootVersion=2.5.4


In this example, the -d options specify the desired project configurations:

  • dependencies: The dependencies you want to include in your project. In this example, web and devtools are selected.
  • language: The programming language for your project. In this example, java is selected.
  • type: The build system for your project. In this example, maven-project is selected.
  • javaVersion: The Java version to use in your project. In this example, Java 11 is selected.
  • bootVersion: The Spring Boot version to use in your project. In this example, version 2.5.4 is selected.


After running the command, a ZIP file named my-spring-app.zip will be downloaded, which contains the generated project structure for your Spring Boot application.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To stop a Spring Boot application from the command line, you need to perform the following steps:Identify the process ID (PID) of the running Spring Boot application. You can do this by using the jps command. Open the command prompt and type jps -l. Look for t...
To integrate Spring Boot with Angular, the following steps can be followed:Create a new Spring Boot project: Start by setting up a new Spring Boot project using your preferred IDE or Spring Initializer. Include the necessary dependencies for web and data. Set ...
To connect Spring Boot to MySQL, you need to follow these steps:First, make sure you have MySQL installed and running on your system. In your Spring Boot project, open the application.properties file. Add the following properties to the file: spring.datasourc...