Running a Spring Boot application starts your program so it can listen for requests and do its work.
Running a Spring Boot application in Spring Boot
mvn spring-boot:run # or if using Gradle ./gradlew bootRun # Or run the main class directly java -jar target/your-app.jar
You can use Maven or Gradle commands to start the app during development.
After building, you can run the packaged jar file with java -jar.
mvn spring-boot:run
./gradlew bootRun
java -jar target/myapp-0.0.1-SNAPSHOT.jarThis is a minimal Spring Boot application. The @SpringBootApplication annotation sets up the app. The main method starts it.
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Make sure you have Java installed and your build tool (Maven or Gradle) set up.
The console shows logs when the app starts, including a message that it started successfully.
You can stop the app anytime by pressing Ctrl+C in the terminal.
Running a Spring Boot app means starting it so it can do its work.
You can run it using Maven, Gradle, or by running the jar file.
The main method with SpringApplication.run() is the entry point.
Practice
Solution
Step 1: Understand the role of the main method
The main method is the entry point of any Java application, including Spring Boot apps.Step 2: Identify what SpringApplication.run() does
SpringApplication.run() starts the Spring Boot application by initializing the Spring context and components.Final Answer:
To start the Spring Boot application using SpringApplication.run() -> Option BQuick Check:
Main method starts app = To start the Spring Boot application using SpringApplication.run() [OK]
- Confusing main method with configuration tasks
- Thinking main method defines endpoints
- Assuming main method creates UI views
Solution
Step 1: Recall Maven command for Spring Boot
The standard Maven command to run a Spring Boot app is 'mvn spring-boot:run'.Step 2: Check other options for correctness
Other options do not follow Maven or Spring Boot plugin syntax and will cause errors.Final Answer:
mvn spring-boot:run -> Option AQuick Check:
Maven run command = mvn spring-boot:run [OK]
- Mixing up command order
- Using invalid Maven goals
- Typing incomplete commands
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}Solution
Step 1: Analyze the main method code
The main method calls SpringApplication.run() with the class annotated by @SpringBootApplication.Step 2: Understand @SpringBootApplication effect
This annotation enables auto-configuration and component scanning, so Spring Boot context starts properly.Final Answer:
The application starts and initializes Spring Boot context -> Option AQuick Check:
SpringApplication.run() starts app = The application starts and initializes Spring Boot context [OK]
- Thinking @SpringBootApplication is optional
- Assuming code throws error without extra config
- Believing app runs but does not start Spring context
java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication. What is the most likely cause?Solution
Step 1: Understand the error meaning
NoClassDefFoundError means the JVM cannot find the SpringApplication class at runtime.Step 2: Identify cause of missing class
This usually happens if the Spring Boot dependency is not included or not properly loaded in the project.Final Answer:
The Spring Boot dependency is missing from the project -> Option CQuick Check:
NoClassDefFoundError = missing dependency [OK]
- Blaming missing main method for this error
- Thinking empty config files cause class errors
- Assuming Java version causes missing class
app.jar?Solution
Step 1: Recall how to run a jar file in Java
The standard command to run an executable jar is 'java -jar filename.jar'.Step 2: Check other options for correctness
The mvn and gradle commands are for build tools, not for running jar files directly. 'java run app.jar' is invalid Java command syntax.Final Answer:
java -jar app.jar -> Option DQuick Check:
Run jar with 'java -jar' = java -jar app.jar [OK]
- Using build tool commands to run jar directly
- Omitting '-jar' option
- Typing invalid Java commands
Ready to prove it?
Pass the test and this pattern moves unknown
