Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

How to Run Spring Boot Application?

Running a Spring Boot application is straightforward, and there are multiple ways to do so. Here's how you can run your Spring Boot application:

1. Using the Maven Plugin:

If you used Spring Initializr to bootstrap your project with Maven, it would include the Spring Boot Maven plugin by default. Navigate to the root directory of your project using a terminal or command prompt and execute:

mvn spring-boot:run

This will start your Spring Boot application.

2. Using the Gradle Plugin:

If your project uses Gradle, you can run:

./gradlew bootRun

(For Linux or macOS)

Or:

gradlew bootRun

(For Windows)

3. Running as a Packaged Application:

You can also package your application as a JAR or WAR and then run it. To package it as a JAR:

For Maven:

mvn clean package

For Gradle:

./gradlew build

Once packaged, navigate to the target directory (for Maven) or build/libs directory (for Gradle), and you'll find your JAR. You can run it using:

java -jar your-artifact-name.jar

4. Using an IDE:

Most modern IDEs (Integrated Development Environments) like IntelliJ IDEA, Eclipse, or STS (Spring Tool Suite) provide support for Spring Boot applications.

  • In IntelliJ IDEA: Navigate to the main class (the one annotated with @SpringBootApplication) and click on the green arrow (Run) next to the class declaration.

  • In Eclipse or STS: Right-click on the main class and choose 'Run As' -> 'Java Application'.

Tips:

  1. Make sure all your application's dependencies are correctly set up, and there are no build errors before trying to run.
  2. Spring Boot will print out logs on the console. Look out for the log statement that says something like Started YourApplicationName in X seconds. This indicates your application has started successfully.
  3. In case of any issues during startup, the error logs will help you diagnose the problem.

Remember to navigate to the appropriate URL in your web browser (typically http://localhost:8080) to interact with your application, unless you've configured a different port.

  1. Launching a Spring Boot application from the command line:

    • Open a terminal and navigate to the project directory.
    • Use the following command to run the application:
      ./mvnw spring-boot:run
      
    • Alternatively, if using Gradle:
      ./gradlew bootRun
      
  2. Using Maven or Gradle to build and run a Spring Boot application:

    • Maven:
      mvn clean install
      java -jar target/your-application.jar
      
    • Gradle:
      ./gradlew build
      java -jar build/libs/your-application.jar
      
  3. Debugging a Spring Boot application in an IDE or command line:

    • In an IDE, set breakpoints and debug as usual.
    • For command line, add debug options:
      java -jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 your-application.jar
      
  4. Running a Spring Boot application in a Docker container:

    • Create a Dockerfile, build an image, and run the container.
    • Example Dockerfile:
      FROM openjdk:11
      COPY target/your-application.jar /app.jar
      CMD ["java", "-jar", "/app.jar"]