๐Ÿ”ด The Error You're Seeing

Confirm this matches your console output. If it does, you're in the right place.

ERROR LOG*************************** APPLICATION FAILED TO START *************************** Description: Embedded container (Tomcat) 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. org.springframework.boot.web.server.PortInUseException: Port 8080 was already in use. at org.springframework.boot.web.server.PortInUseException.lambda$throwIfAlreadyInUse$1(PortInUseException.java:92) at org.springframework.boot.web.server.PortInUseException$$Lambda/0x00007f5b8c00a040.accept(Unknown Source) at java.base/java.util.Optional.ifPresent(Optional.java:178) at org.springframework.boot.web.server.PortInUseException.throwIfAlreadyInUse(PortInUseException.java:90) at org.springframework.boot.web.server.AbstractConfigurableWebServerFactory.initializeAddress(AbstractConfigurableWebServerFactory.java:147) at org.springframework.boot.web.server.AbstractConfigurableWebServerFactory.getAddress(AbstractConfigurableWebServerFactory.java:118) at org.springframework.boot.web.embedded.tomcat.TomcatWebServerFactory.getWebServer(TomcatWebServerFactory.java:168) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:176) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:614) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) at org.springframework.boot.SpringApplication.run(SpringApplication.java:334) at com.example.demo.DemoApplication.main(DemoApplication.java:10) Caused by: java.net.BindException: Address already in use at java.base/sun.nio.ch.Net.bind0(Native Method) at java.base/sun.nio.ch.Net.bind(Net.java:579) at java.base/sun.nio.ch.Net.bind(Net.java:568) at java.base/sun.nio.ch.ServerSocketChannelImpl.netBind(ServerSocketChannelImpl.java:347) at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:301) at org.apache.tomcat.util.net.NioEndpoint.initServerSocket(NioEndpoint.java:276) at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:231) at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:256) at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:695) at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:658) at org.apache.coyote.ProtocolHandler.start(ProtocolHandler.java:59) at org.apache.catalina.connector.Connector.startInternal(Connector.java:1004) at org.apache.catalina.core.StandardService.startInternal(StandardService.java:355) at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:164) at org.apache.catalina.startup.Tomcat.start(Tomcat.java:502) at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:107) ... 7 common frames omitted

โšก Quick Fix Works 80% of the time

Kill the process on port 8080, or change your app to use a different port.

# Option A: Find and kill the process (macOS / Linux) lsof -i :8080 # Look for the PID in the output, then: kill -9 <PID> # Option B: Change port in application.properties server.port=8081 # Option C: Change port via command line ./mvnw spring-boot:run -Dspring-boot.run.arguments=--server.port=8081

๐Ÿ› ๏ธ Solutions (3 Ways to Fix)

Solution 1โœ“ Most common cause

Kill the occupying process

๐Ÿ‘‰ You have a stray process (another Spring Boot app, Docker container, or service) bound to 8080.

Find the process ID (PID) listening on port 8080 and terminate it. This is the fastest fix when you know the port should be free.

# macOS / Linux lsof -i :8080 # Output: java 12345 user 123u IPv4 ... TCP *:8080 (LISTEN) kill -9 12345 # Windows netstat -ano | findstr :8080 # Look for the PID in the last column taskkill /F /PID 12345
Solution 2

Change the port in properties

๐Ÿ‘‰ You want to run multiple Spring Boot apps simultaneously, or 8080 is reserved by your OS / IT policy.

Override the default port in your config file. Spring Boot picks up server.port at startup and binds Tomcat to that port instead.

# application.properties server.port=8081 # Or application.yml server: port: 8081 # Or environment variable (highest precedence) export SERVER_PORT=8081 java -jar my-app.jar
Solution 3

Use a random port

๐Ÿ‘‰ You are in development / testing and don't care which port the app uses.

Setting server.port to 0 tells Spring Boot to bind to a random available port. Check the logs for "Tomcat started on port(s): <random>" or use server.port=${random.int(1024,65535)} for a wider random range.

# application.properties server.port=0 # Or a random port in a range server.port=${random.int(1024,65535)}

๐Ÿ“‹ Version Notes

Spring Boot 2.x

Same PortInUseException, but the stack trace goes through older Tomcat connector classes (org.apache.coyote.http11.Http11NioProtocol).

Spring Boot 3.x

Stack trace reflects the newer Tomcat 10+ package names (org.apache.coyote.ProtocolHandler). The error message and solution are identical.

๐Ÿง  Why this Happens

Tap to expand the deep technical explanation

Spring Boot's embedded Tomcat server opens a TCP socket on the configured port during application startup. The JVM's `ServerSocketChannel.bind()` makes a system call (`bind()` syscall on Linux/macOS, `bind()` Winsock on Windows). The OS kernel maintains a table of bound ports for all running processes. If any process has already bound a socket to 0.0.0.0:8080 (or to any interface on port 8080), the kernel rejects the second bind attempt with EADDRINUSE ("Address already in use"). Java translates this into a `BindException`, which Tomcat wraps in its own exception chain, and Spring Boot surfaces it as a `PortInUseException` with the descriptive message you see in the failure report.

The HITEC City Parking Spot Analogy:

Imagine you walk into a co-working space and try to sit at Desk 8080. Someone is already sitting there. You have two choices: ask them to leave (kill the process), or pick a different desk (change the port). The co-working manager (Spring Boot) tells you exactly that.

๐Ÿ›ก๏ธ How to Prevent This Next Time

Always reserve a port range for your development machine and document which apps use which ports. Use Docker Compose with explicit port mappings to avoid conflicts. For microservices, use a port registry or let each service pick a random port and discover each other via service discovery (Eureka / Consul).

Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase