This article tries to answer one of the most often occurred questions connected with Spring Boot development. When developing a web application with Spring Boot, you might hit an error announcing that port 8080 is occupied. In this article, we will see why and how to solve this error. However, be sure that tutorial can help you solve the occupancy of any other port.
Spring Boot application default port
When you develop long enough with Spring Boot, you know the error very well. You work on your Spring Boot application, and you use a console for running the application. Or you use your favourite IDE runner for starting your application (my preferred IDE is IntelliJ IDEA). Your Spring Boot application in the running embedded in servlet container when suddenly you need to do something else. Instead of adequately terminating the embedded servlet container with your application, you detach from the servlet by a closing terminal window or switching to a different project in IDE. However, because you did not correctly terminate your IDE runner and did not correctly kill the terminal’s application process, the servlet with the application is still running. After that, you might, of course, start developing something else. Surprise, surprise, you cannot. Any Spring Boot application port, by default number 8080, is still occupied with the unterminated process of your Spring Boot servlet container. (In the tutorial, we assume you are using the Spring Boot application port on default port number 8080).
I guess you might be a little bit confused by all the wording describing the issue. Let’s take a look first at some terminology to understand the problem here fully:
What is process?
We are aware that readers of this blog are pretty skilled in computers. So, we will provide a brief description of what is computer process without going to further details.
Every time you start a program on your computer, it requires a certain amount of resource. Your Central Processor Unit, known as CPU, add to this program its process.
If you open task manager on your Windows, you will see a list of all process your CPU is trying to manage at once.
What is process ID?
The process ID, also known as PID, is the number assigned by the operating system to each new process.
What is port number?
A port number is how to identify individual processes for network message when they arrive on a server. In a real sense, a port number is the logical address of each application or process that helps identify the sender and receiver of processed messages.
You can imagine port numbers as doors in the hallway into which message flows through the networks. It leaves one door and enters another.
What is Spring Boot server container?
Spring Boot is a Spring project which makes Spring development fast. Without going too much into the Spring Boot project details, your Spring Boot application runs in a server container, better known as a servlet. This servlet runs in one computer process and lister on port 8080 for traffic.
Restart computer
One way how to solve this is to restart your computer. During the restart of Windows OS, start all the fresh, and port 8080 will be empty. But this is not the fastest and definitely not the right way for developers to solve this.
Set different port number
Another way is to solve this issue by assigning the Spring Boot application a different port number. When we set a different port number for the application, we will not kill the existing process with the application servlet container. However, we will create a new-different process for the servlet container, which will be listening on a different port. In this way, port 8080 will be still occupied, but we will at least be able to continue to develop.
Go to Spring Boot application.properties
file and set different port number for server.port
.
Despite this quick workaround, there is a much more mature way to solve this problem. I would like you to show a better way to kill a Spring Boot servlet container listening on port 8080.
Kill the process listening at port 8080
To kill the process listening at port 8080 in the Windows environment, we need only go through a few steps. First, check the image below to get the idea.
Find the process occupying port 8080
On the Windows, open your CMD terminal. With simple command netstat -ano | findstr PORT_NUMBER
, where for PORT_NUMBER add port number 8080, and on command prompt output (hit enter), we will get the process id of process listening on port 8080.
Kill the process with command prompt
Now, all we need to do is just type the following command taskkill /F /PID PROCESS_ID
and replace PROCESS_ID by the process id we got from the previous command result.
If you have typed everything correctly, process listening on port 8080 should be terminated.