How to Fix Port Already in Use Error in Spring Boot
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.
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
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.
server.port=8081Prevention
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.