0
0
SpringbootDebug / FixBeginner · 3 min read

How to Fix Port Already in Use Error in Spring Boot

The port already in use error in Spring occurs when the default port 8080 is occupied by another process. Fix it by changing the port in application.properties or stopping the process using the port.
🔍

Why This Happens

This error happens because Spring Boot tries to start the web server on a port that is already taken by another application or process. By default, Spring Boot uses port 8080. If another program is using this port, Spring cannot start and throws an error.

text
2024-06-01 10:00:00.000 ERROR 12345 --- [           main] o.s.b.d.a.OptionalLiveReloadServer       : Port 8080 is already in use
org.springframework.boot.web.server.PortInUseException: Port 8080 is already in use
Output
org.springframework.boot.web.server.PortInUseException: Port 8080 is already in use
🔧

The Fix

You can fix this by changing the port Spring Boot uses or by stopping the process that occupies the port. To change the port, add or update the server.port property in src/main/resources/application.properties. For example, set it to 8081 to use a free port.

properties
server.port=8081
Output
Spring Boot application starts successfully on port 8081
🛡️

Prevention

To avoid this error in the future, always check which ports are in use before starting your Spring Boot app. Use commands like lsof -i :8080 (Linux/macOS) or netstat -ano | findstr :8080 (Windows) to find and stop conflicting processes. Also, consider configuring your app to use a configurable port via environment variables or profiles.

⚠️

Related Errors

Other common errors include Address already in use when binding sockets and Failed to start embedded Tomcat due to port conflicts. The fixes are similar: change the port or stop the conflicting process.

Key Takeaways

The default Spring Boot port 8080 may be occupied causing startup failure.
Change the port in application.properties using server.port to fix the conflict.
Use system commands to find and stop processes using the desired port.
Configure ports dynamically with environment variables for flexible deployments.
Similar errors usually relate to socket binding and can be fixed the same way.