Launching Spring Application: Fixing “Address Already in Use” Error

Launching Spring Application: Fixing “Address Already in Use” Error

When launching a Spring Boot application, you might encounter the error “Address already in use”. This typically occurs when the default port (8080) is already occupied. Here are some solutions to resolve this issue:


Solution 1: Change the Application Port

Spring Boot applications use embedded Tomcat servers by default. You can easily change the port as follows:

Option 1: Use Command Line

Run the following command to specify a different port:

java -jar target/gs-serving-web-content-0.1.0.jar --server.port=8181

Option 2: Update Configuration

Modify your application.properties or application.yml file to set a new port:

server.port=8181

Solution 2: Kill the Process Using the Port

Identify and stop the process occupying the port (e.g., 8080).

On Linux/MacOS:

  1. Find the process:
   lsof -i :8080 | grep LISTEN

Example output:

   java    78960 xyxss  119u  IPv6 0x6c20d372bc88c27d      0t0  TCP *:8080 (LISTEN)
  1. Kill the process:
   kill -9 78960

On Windows:

  1. Find the process:
   netstat -ano -p tcp | find "8080"

Example output:

   TCP    0.0.0.0:8080       LISTENING       1234
  1. Kill the process:
   taskkill /F /PID 1234

Solution 3: Verify Port Usage

Option 1: Use Telnet

Run:

telnet localhost 8080

or visit:

http://localhost:8080

If the port is in use, change the port in your application.properties file:

server.port=8181

Option 2: Check with Netstat

On Linux/MacOS:

netstat -pnltu | grep -i "8080"

On Windows:

netstat -ano -p tcp | find "8080"

Solution 4: Modify IDE Configuration

If you are running your application via an IDE like IntelliJ IDEA, you can modify the port in the Run Configuration:

  1. Go to Run → Edit Configurations.
  2. Add --server.port=8181 to the program arguments.

Solution 5: Use Maven

Run the application with a custom port using Maven:

mvn spring-boot:run -Dserver.port=8181

Extra Tips

Sometimes, you may need to change other related ports:

management.port=8182
tomcat.ajp.port=8009

Additionally, ensure no other services like Docker or another Tomcat instance are using the port.


#SpringBoot #Java #Tomcat #SpringError #PortConflict #JavaDevelopment #SpringBootSolutions #SpringApplicationError #ProgrammingTips #BackendDevelopment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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