Last week I wrote an article on the improvements in Spring Boot 1.4 of startup errors. Specifically, we saw that we got a really informative error message when we try and run an application and port 8080 is already in use. I received a question from a reader that went like this 'I get the port 8080 is already in use error from time to time and I am not sure how to fix it, what can I do?' This is actually pretty easy to fix and happens to all of us. In this short article, I will show you how to do it on macOS and link to another article I wrote on how to do this on Windows.
Port 8080 is already in use
From the Terminal, type the following command: pkill 'Example Process name agent'. Hit Return to immediately terminate the named process. Like killall, pkill will immediately terminate the process that has been targeted with no confirmations, dialogs, saves, or anything else.
- Using pkill to Kill Processes on Mac OS. At it’s most basic function, pkill can be used as follows: pkill ApplicationName. For example, killing all processes belonging to “Safari”, including Safari Web Content processes, would be just a matter of typing: pkill Safari. Killing Processes with pkill and Wildcards.
- Open Windows Terminal. Type the following command to show processes running on the port you're looking to kill processes. Netstat -ano findstr:PORT. Type following to kill the process. Kill PID For Example: PS C: Users username netstat -ano findstr:4445 TCP 0.0.0.0:4445 0.0.0.0:0 LISTENING.
This problem usually comes up because another process wasn't properly terminated. If you want to simulate this issue you can fire up one app in a terminal and then try launching another in your favorite IDE (IntelliJ right?). I opened up a terminal and ran my other project using the Maven plugin.
Now if we head into IntelliJ and try to run our application we are going to see an error that looks something like this.
Activity Monitor FTW
So we know that another process is running but it isn't always as simple as forgetting a terminal window is open and running another app. This can happen when an IDE is closed and the process is correctly terminated. macOS gives us a nice tool for monitoring process by name and port called Activity Monitor. If you open up Activity Monitor (I just use Spotlight) you could manually look for the processes or use the search bar in the upper right-hand corner. Now you can kill the existing processes and your unwanted process will be gone.
Mac Find Process Using Port
What about Windows?
If you're a windows user I wrote an article awhile back on how to kill a process by port number.