🏠Spring BootWeb server failed to start Port 8080 was already in use

Web server failed to start Port 8080 was already in use

In this post, We will try to understand the Web server failed to start Port 8080 was already in use error and how to fix it.

Why Port 8080 was already in use?

In the network, an IP address identifies each machine. Similarly, The network port identifies the application or process running on a machine. When an application wants to use a port, the OS binds one. However, when multiple applications want to use the same port, there is a port clash.

In our case, port 8080 was already being used by another application and hence the web server failed to start. Usually, you would get this error in the case of port 8080, 8081, 9090 etc. So in general, If you get a “port 8080 was already in use” error, then it is certain that another application is already using that port.

This is most likely due to bad configuration from your end, running the application multiple times or not using a proper startup and shutdown scripts.

Fix for Web server failed to start

As we know, The cause is that another process is running on the same port. To solve this, you have two options.

  1. Try to run the application on a port other than 8080.
  2. Identify and stop the process running on that specific port

Option 1: Run your web server on a different port

Most of the application frameworks provide options to change the ports they listen to. For instance, you can change the application port of a spring boot application in the following ways.

You can provide a server.port configuration with a different port number in the application.properties file.

server.port=9090
Code language: Properties (properties)

You can also pass the port number as an application parameter.

java - jar my-server.jar --server.port=9090
Code language: Bash (bash)

Or a JVM parameter.

java - jar -Dserver.port=9090 my-server.jar
Code language: Bash (bash)

This way, The application starts on a different port. Thus the “Web server failed to start. Port 8080 was already in use” error is avoided.

There is a detailed post on how to change the default tomcat port number in 5 different ways. You can also find more details on how the spring boot configuration works at their official documentation.

Option 2: Kill the server running on port 8080

Sometimes, the other process is just an old instance of the same application or an application that you don’t want to run. In these cases, it is best to identify and kill them so that you can start your own application on that specific port. To do that you need to first identify the process. Second, you need to kill it.

Say you got the error for running on port 8080. Then you should use the below command for identifying the process or service.

Finding and killing process running on port 8080 on Linux/Mac

One of the following commands should give the process ID (PID) of the application or service running on port 8080.

sudo lsof -n -i :8080 | grep LISTEN sudo netstat -nlp | grep :8080 sudo ss -lptn 'sport = :8080'
Code language: Bash (bash)
python http server running on port 8080 with PID 25321

With the PID from the above output, you can kill the process using the following commands.

#to kill process gracefully kill -15 25321
Code language: Bash (bash)

or

#To force kill a process kill -9 25321
Code language: Bash (bash)

By killing the process occupying the port on 8080, the web server can start without any problems.

For your use, replace the process id with the process id that you found from your machine.

Finding and killing process running on port 8080 on windows

Similarly, You can run the following command to identify a process running on a port in windows.

netstat -ona | findstr :8080 | findstr LISTENING
Code language: Bash (bash)
process 23928 listening on port 8080

Once you get hold of the process id, you can use the following command to kill it.

taskkill /F 25321
Code language: Bash (bash)

As I said earlier, for your machine, the process ID may be different.

Conclusion

So we learned ports work and how to solve “Port 8080 was already in use” errors on application startup. If you liked this article, then you might like to read about the following titles.

Similar Posts

Leave a Reply

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