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.

Web server failed to start. Port 8080 was already in use

Why?

In network, an IP address identifies each machines. 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 port clash.

If you get a Port XXXX was already in use error, then it is certain that another application is already using the 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.

How to Fix it?

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 different port
  2. Identify and stop the process running on that specific port

Option 1: Run your application 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

You can also pass the above configuration as an application parameter.

java - jar my-server.jar --server.port=9090

Or a JVM parameter.

java - jar -Dserver.port=9090 my-server.jar

You can find more details on how the spring boot configuration works at their official documentation.

If you have not noticed before, this approach is probably the cleaner way to avoid port conflicts. As this approach doesn’t affect other processes, you should always use this method.

Option 2: Identify and stop the other processes

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, its 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.

For Linux machines

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'
find process running on port 8080 on a linux server
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

or

#To force kill a process
kill -9 25321

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

For windows machines

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

netstat -ona | findstr :8080 | findstr LISTENING
find process running on port 8080 on windows
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

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.

Leave a Comment