Sub topic 10
Running the Application
This chapter covers how to start a generated Spring Boot project, what happens during startup, and how to read the output. It assumes you can read basic Java, have used Maven at a user level, understand HTTP at a basic level, and have a JDK and a terminal installed. It also assumes you generated a project with Spring Initializr and have read the build file and the main class in earlier chapters.
Three Ways to Run a Boot App
A Spring Boot application is an ordinary Java program with a main method. You can start it in several ways, and each fits a different situation.
| Method | Command or action | Typical use |
|---|---|---|
| Build tool plugin | `./mvnw spring-boot:run` | Daily development from a terminal |
| IDE run configuration | Run the `main` class | Development with a debugger |
| Executable JAR | `java -jar target/demo-0.0.1-SNAPSHOT.jar` | Testing the deliverable, deployment |
All three end up calling SpringApplication.run(...) in your main class. The difference is how the classpath is built. The plugin and the IDE use the compiled classes and dependency JARs on disk. The executable JAR carries everything inside one archive. The packaging chapter covers that archive in detail. Here we focus on running from the plugin and from the JAR.
Running with the Maven Wrapper
Generated projects include the Maven wrapper scripts mvnw (macOS and Linux) and mvnw.cmd (Windows). The wrapper downloads the Maven version the project expects, so you do not need to install Maven yourself. Run these commands from the directory that contains pom.xml.
./mvnw spring-boot:runOn Windows, use mvnw.cmd spring-boot:run. On macOS and Linux, if you get "permission denied", make the script executable with chmod +x mvnw.
The spring-boot:run goal comes from the Spring Boot Maven plugin, which the generated pom.xml already declares. The goal compiles the project if needed and then starts the application in a forked JVM. The first run is slow because Maven downloads the plugin and all dependencies into your local repository (~/.m2). Later runs are much faster.
Stop the application with Ctrl+C. Spring Boot registers a JVM shutdown hook, so the application context closes and releases resources such as the server port.
Reading the Startup Log
A healthy startup prints a banner and a series of log lines. The exact text varies by version, but the structure is stable. A trimmed example for a web project looks like this:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
INFO com.example.demo.DemoApplication : Starting DemoApplication using Java 21 with PID 4821
INFO com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default"
INFO o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
INFO o.apache.catalina.core.StandardService : Starting service [Tomcat]
INFO o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
INFO com.example.demo.DemoApplication : Started DemoApplication in 1.9 seconds (process running for 2.3)Read the lines in order:
- Starting shows the main class, the Java version, and the process ID.
- Active profile shows which profiles are enabled. With none set, Boot uses
default. - Tomcat initialized appears because a web starter put an embedded server on the classpath. It reports the port. This line is a quick check that auto-configuration created the web server.
- Started means the application context finished refreshing: beans were created, wired, and the server is accepting requests.
The sequence matches the internal flow of SpringApplication.run.
placeholderBecause the example diagram below must follow the required format, here it is in the correct form.
What Keeps the Process Alive
A plain Java program exits when main returns. Your main method returns right after SpringApplication.run finishes, yet the process keeps running. The reason is the embedded server. Tomcat starts non-daemon threads that listen on the port, and the JVM stays alive while those threads run.
This explains a common surprise. If you create a project with no web starter, for example only spring-boot-starter, the application starts, runs any startup code, logs "Started", and exits immediately. Nothing is holding the JVM open. That behavior is correct for command-line tools and batch jobs.
Testing That It Works
With the application running, open a second terminal. Once you have added the endpoint from the next chapter, call it like this:
curl http://localhost:8080/helloBefore you add any controller, a request to the root path still gives you useful information. Boot's default error handling answers with a JSON body and a 404 status:
curl -i http://localhost:8080/The -i flag prints the response headers. A 404 with a JSON body that contains "status":404 and "error":"Not Found" proves the server is up and Boot's error handling is active. A "connection refused" message means nothing is listening on that port.
Running the Packaged JAR
The development goal is convenient, but the JAR is what you deploy. Build it and run it directly:
./mvnw clean package
java -jar target/demo-0.0.1-SNAPSHOT.jarThe file name comes from the artifactId and version in pom.xml. If you changed either value, adjust the command. The JAR runs without Maven, so a machine only needs a compatible JDK or JRE. Running it once on your own machine catches problems that spring-boot:run can hide, such as resources that exist only in your source tree.
Changing Behavior at Launch
You do not need to edit files to change common settings. Boot reads command-line arguments, environment variables, and application.properties, in a defined order of precedence. Command-line arguments win over the properties file.
java -jar target/demo-0.0.1-SNAPSHOT.jar --server.port=9090With the Maven plugin, pass arguments through a property:
./mvnw spring-boot:run -Dspring-boot.run.arguments=--server.port=9090Environment variables work too. Boot maps SERVER_PORT=9090 to server.port. The later configuration chapter explains the full precedence order.
Common Startup Failures
Most failed starts fall into a few categories. Boot prints a readable "failure analyzer" block for many of them, with a description and an action.
Port Already in Use
***************************
APPLICATION FAILED TO START
***************************
Description:
Web server failed to start. Port 8080 was already in use.
Action:
Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.This often means a previous run is still alive, perhaps in another terminal or an IDE. Stop that process, or start this one on another port with --server.port=8081. Setting server.port=0 asks the operating system for any free port, which is useful in tests.
Wrong Java Version
An error such as class file version 65.0, this version of the Java Runtime only recognizes class file versions up to 61.0 means the project was compiled for a newer Java than the one running it. Check the version with java -version and ./mvnw -v. Make sure JAVA_HOME points at a JDK that meets the java.version value in pom.xml.
Missing or Conflicting Beans
Messages such as No qualifying bean of type ... or Parameter 0 of constructor ... required a bean of type ... that could not be found indicate a class that Spring cannot see. The usual cause is a class outside the package of the main class. Component scanning starts at that package and covers only its sub-packages, so a controller in org.other is never found when the main class lives in com.example.demo.
Database Configuration Missing
If you add a data starter such as JPA without a database driver or URL, auto-configuration tries to create a DataSource and fails with a message about a missing URL or driver. Either add the driver and set spring.datasource.url, or remove the starter you do not need.
Seeing What Boot Configured
When the startup behavior is unclear, ask Boot to explain its decisions. Run with the debug flag:
java -jar target/demo-0.0.1-SNAPSHOT.jar --debugThis prints a conditions evaluation report. It lists auto-configurations that matched ("Positive matches"), those that did not and why ("Negative matches"), and classes that were excluded. It is the most direct way to answer "why is this bean here?" or "why was that bean not created?".
Rebuilding After a Change
spring-boot:run does not watch your files. After editing code you stop the process with Ctrl+C and start it again. The optional DevTools module, mentioned at the end of this tutorial, adds automatic restarts. Until then, the loop is: edit, stop, run, test with curl.
If you run from an IDE, a rebuild happens automatically when you press Run. If you run the JAR, you must rerun ./mvnw clean package first, because the JAR does not change when you edit source files. A frequent mistake is editing code and rerunning an old JAR, then wondering why the change has no effect.
Exercise
You run java -jar target/demo-0.0.1-SNAPSHOT.jar on a project that has only spring-boot-starter (no web starter). The log ends with "Started DemoApplication" and the process returns to the shell prompt. Why does it exit, and what change would keep it running as a web server? Also name the single launch argument you would add to start it on port 9090 once it is a web application.
Show answer
With no web starter there is no embedded server, so no non-daemon thread keeps the JVM alive. main returns after SpringApplication.run and the JVM exits. Adding spring-boot-starter-web to the dependencies puts Tomcat on the classpath, so auto-configuration creates the embedded server and its listener threads keep the process running. To use port 9090, add --server.port=9090 after the JAR name.
Exercise
Your application fails to start with "Port 8080 was already in use." Give two different ways to resolve it without editing Java code.
Show answer
Stop the process that is using port 8080, for example a previous run in another terminal. Or start this application on a different port, either with --server.port=8081 on the command line or by setting server.port=8081 in application.properties.