“That dreaded ‘Address already in use’ error is like showing up to your favorite café only to discover someone’s already perched in your seat—time to politely ask them to move!” 🪑✨


🧐 Q&A: Fixing the “port already in use” error

Q1: Why does Spring Boot complain about “Port 8080 already in use”?
A: Every TCP port can host only one service at a time. If you’ve got another app—perhaps a previous run of your own service, Docker container, or even a database UI—listening on 8080, Spring Boot won’t be able to bind and will fail fast with:

perlCopyEditjava.net.BindException: Address already in use: bind

Q2: How do I find out who’s squatting on port 8080?

  • macOS/Linux bashCopyEditlsof -i :8080 # or sudo netstat -plnt | grep 8080
  • Windows (PowerShell/CMD) powershellCopyEditnetstat -ano | findstr 8080 tasklist /FI "PID eq <pid>"

These commands reveal the process ID (PID) and program name—your culprit!


Q3: How can I evict the squatter?

  • macOS/Linux bashCopyEditkill <PID> # Graceful kill -9 <PID> # Force-quit if stubborn
  • Windows powershellCopyEdittaskkill /PID <PID> /F

Q4: Can I just run my app on a different port?
Absolutely! In src/main/resources/application.properties (or application.yml):

propertiesCopyEditserver.port=9090

Or override on the fly:

bashCopyEdit./mvnw spring-boot:run -Dspring-boot.run.arguments="--server.port=9090"

Now your app lands on port 9090 instead of the crowded 8080!


Q5: Any Spring-specific tricks to avoid this in development?

  • Random ports for tests: javaCopyEdit@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
  • Profile-based ports: propertiesCopyEdit# application-dev.properties server.port=0 # JVM assigns a free port automatically Retrieve the actual port at runtime via @LocalServerPort.

Q6: How do I permanently prevent conflicts?

  1. Single source of truth: centralize your ports in application.yml under named profiles.
  2. CI/CD checks: add a startup smoke test in your pipeline to catch bind exceptions early.
  3. Container isolation: run microservices in Docker with mapped host ports to avoid overlap.

🚀 Monkey-Proof Challenge

  1. Trigger the error by running two instances of your app simultaneously.
  2. Identify the rogue process with lsof or netstat.
  3. Fix it by killing the process and/or switching to port 0 in application-dev.properties.
  4. Automate a test: write a JUnit test that starts your app on a random port and asserts it’s listening.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *