Sub topic 10
Running the Application
This chapter assumes you have a generated Spring Boot project (Maven, with the wrapper files mvnw and .mvn/), a JDK installed, and basic familiarity with a terminal. You will start the app, read its startup log, call it, and fix the most common startup errors.
Two ways to run during development
The generated project includes the Maven wrapper, so you do not need Maven installed. From the project root, run:
./mvnw spring-boot:runOn Windows, use mvnw.cmd spring-boot:run. The spring-boot-maven-plugin in the build file provides this goal. It compiles your code, builds the classpath, and calls the main method of your application class.
You can also run the main method of DemoApplication from your IDE. Both approaches run the same SpringApplication.run(...) call. Use the one that fits your workflow.
What happens at startup
SpringApplication.run does several things in order:
- It creates an application context, the container that holds your beans.
- It reads configuration, including
application.properties. - It applies component scanning and auto-configuration, which were enabled by
@SpringBootApplication. - It starts the embedded web server, because the web starter is on the classpath.
Reading the startup log
The console output follows a fixed pattern. These lines matter most:
Starting DemoApplication using Java 21 with PID 4821
No active profile set, falling back to 1 default profile: "default"
Tomcat initialized with port 8080 (http)
Tomcat started on port 8080 (http) with context path '/'
Started DemoApplication in 1.9 seconds (process running for 2.3)Your versions and timings will differ. The line Tomcat started on port 8080 tells you the server is listening. The final Started DemoApplication line confirms the context finished loading.
If the app has no web starter, it starts, runs, and exits. With a web starter, the server threads keep the process alive until you stop it with Ctrl+C.
Calling the app
With the HelloController from the next chapter in place, open a second terminal:
curl http://localhost:8080/helloThe response is Hello, Spring Boot!. Without any controller, the same request returns a JSON error with status 404. That is normal and shows the server is running.
Common startup errors
| Symptom | Cause | Fix |
|---|---|---|
| `Port 8080 was already in use` | Another process uses the port | Stop that process or set `server.port` |
| `release version 21 not supported` | The JDK is older than the `java.version` in the build file | Install a matching JDK or lower `java.version` |
| 404 on your endpoint | Controller is outside the scanned package | Move it under the main class's package |
| App starts and exits at once | No web starter on the classpath | Add `spring-boot-starter-web` |
| `Failed to configure a DataSource` | A database starter is present but no URL is set | Set `spring.datasource.url` or remove the starter |
Stopping and restarting
Press Ctrl+C to stop the app. Spring Boot shuts the context down in an orderly way and closes the server. Code changes need a restart unless you add DevTools, which a later chapter covers.
Exercise
You run ./mvnw spring-boot:run and the log ends with Started DemoApplication, but curl http://localhost:8080/hello returns 404. Name two likely causes.
Show answer
The controller may be in a package outside the main class's package, so component scanning never found it. Alternatively, the app may be listening on a different port, such as when server.port is set in application.properties. Check the Tomcat started on port line in the log to confirm the port. Also confirm that the controller has @RestController and a matching @GetMapping("/hello").
Exercise
Which log line tells you the embedded server is accepting connections, and which tells you the application context has finished loading?
Show answer
Tomcat started on port 8080 (http) shows the server is listening. Started DemoApplication in ... seconds shows the context finished loading.