Port already in use in Spring boot application

42 Views Asked by At

I am attempting to run my Spring Boot application as a Spring Boot app, but I am consistently receiving a 'port already in use' exception for every port.

I have attempted to change the port in the application.properties file, but the exception continues to appear for every port.

2

There are 2 best solutions below

0
Anthony Nguyen On

To check listening port you can use below command:

Choose one port not using.

Then set server.port=<your-port> in application.properties

0
Yaseen On

To change the default port (8080) that Spring Boot uses to run an application, you can modify the application.properties or application.yml file by defining the server.port. Here's an example for both file types:

For application.properties file:

server.port=8081

For application.yml file:

server:
  port: 8081

Alternatively, if the default port (8080) is already in use, you may need to kill the service running on that port before starting your Spring Boot application on the new port. Use the following commands in a terminal on macOS or Linux:

  1. Find the Process ID (PID) of the Service Running on Port 8080:

    lsof -i :8080
    

    Look for the PID associated with the process.

  2. Kill the Process Using the PID:

    kill -9 <PID>
    

    Replace <PID> with the actual process ID obtained from the previous command.

For example:

kill -9 12345

This will forcefully terminate the process using port 8080, allowing you to start your Spring Boot application on the new port without conflicts.