Module: Spring Boot Fundamentals

Running Spring Boot Applications

Running Spring Boot Applications

Now that you've created a basic Spring Boot project, let's explore how to run it. Spring Boot provides several convenient ways to execute your application, catering to different development and deployment scenarios.

1. Running from an IDE (IntelliJ IDEA, Eclipse, VS Code)

This is the most common and convenient method during development. Most popular IDEs have built-in support for Spring Boot.

  • IntelliJ IDEA:
    1. Open your project.
    2. Locate the main application class (the one with the @SpringBootApplication annotation).
    3. Right-click within the class and select "Run 'YourApplicationName'". IntelliJ IDEA will automatically detect the main method and run the application.
  • Eclipse:
    1. Open your project.
    2. Locate the main application class.
    3. Right-click within the class and select "Run As" -> "Spring Boot App".
  • VS Code:
    1. Ensure you have the Spring Boot Extension Pack installed.
    2. Open your project.
    3. Locate the main application class.
    4. Right-click within the class and select "Run Java". VS Code will use the Spring Boot extension to run the application.

The IDE will typically start an embedded Tomcat server (or another supported server) and deploy your application. You'll see output in the console indicating the server is starting and eventually that your application is running.

2. Running from the Command Line with Maven

If you're using Maven, you can run your Spring Boot application from the command line using the mvn spring-boot:run command.

  1. Open a terminal or command prompt.

  2. Navigate to the root directory of your Spring Boot project (the directory containing the pom.xml file).

  3. Execute the following command:

    mvn spring-boot:run
    

    Maven will download dependencies (if necessary), compile your code, and start the embedded server. The application's logs will be displayed in the console.

3. Running from the Command Line with Gradle

If you're using Gradle, you can run your Spring Boot application from the command line using the ./gradlew bootRun command.

  1. Open a terminal or command prompt.

  2. Navigate to the root directory of your Spring Boot project (the directory containing the build.gradle or build.gradle.kts file).

  3. Execute the following command:

    ./gradlew bootRun
    

    Gradle will download dependencies (if necessary), compile your code, and start the embedded server. The application's logs will be displayed in the console. (On Windows, use gradlew bootRun instead of ./gradlew bootRun).

4. Running as a JAR File

Once your application is built, you can package it as an executable JAR file. This is the typical way to deploy Spring Boot applications.

  1. Build the JAR file:

    • Maven: mvn clean package
    • Gradle: ./gradlew build (or gradlew build on Windows)
  2. Locate the JAR file: The JAR file will be created in the target directory (Maven) or build/libs directory (Gradle). The filename will typically be something like your-application-name-1.0.0.jar.

  3. Run the JAR file:

    java -jar your-application-name-1.0.0.jar
    

    This will start the embedded server and run your application.

Accessing Your Application

After running your application using any of the methods above, you can access it in your web browser or using a tool like curl. By default, Spring Boot applications run on port 8080.

  • Example: If your application has a controller that maps requests to /hello, you can access it by opening http://localhost:8080/hello in your browser.

Changing the Port

You can change the default port by setting the server.port property in your application.properties or application.yml file.

application.properties:

server.port=8081

application.yml:

server:
  port: 8081

Now your application will run on port 8081, and you can access it at http://localhost:8081/hello.

Summary

You've learned several ways to run your Spring Boot application:

  • From an IDE: The easiest method for development.
  • From the command line (Maven/Gradle): Useful for building and running without an IDE.
  • As an executable JAR file: The standard way to deploy Spring Boot applications.

Understanding these methods will allow you to develop, test, and deploy your Spring Boot applications effectively. In the next section, we'll dive into Spring Boot's auto-configuration features.