How to Fix the Java.Net.BindException: Address Already in Use: Bind

Sheeraz Gul Feb 02, 2024
  1. Fix java.net.BindException: Address already in use: Bind in Java
  2. Solution 1: Run the Server on Different Port
  3. Solution 2: Kill the Previous Process
How to Fix the Java.Net.BindException: Address Already in Use: Bind

This tutorial demonstrates the java.net.BindException: Address already in use: Bind error in Java.

Fix java.net.BindException: Address already in use: Bind in Java

The error java.net.BindException: Address already in use: Bind occurs when one port is used by more than one application as it also states in the error that the address is already in use. The exception states that other processes already use a port you are trying to use.

To solve this error, we can run our server on another port or kill the previous process on the port. To solve this issue, we must know which processes are running on the given port.

Find the Conflict Process on the Port

If the java.net.BindException: Address already in use: Bind error is occurring for your port, then you can find what other processes are running on your port. We can use the netstat command to know the current TCP/IP connections.

Here is the use of netstat for different platforms.

Windows:

For example, our port is 8080; in Windows, the output for the netstat command will give us the process id in the last column for the process running on port 8080. See the command:

netstat -ano | find "8080"

The output for the command is:

TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 12345

The process 12345 is running on port 8080.

Mac OS:

For Mac OS, the command is different from the netstat. Instead of netstat, we use lsof to check the process running on the given port.

See the command:

lsof -t -i:8080

The output for this command will show the process ID only.

12345

Unix/Linux:

The command for Linux/Unix will be:

netstat -an | grep "8080"

The output here will also be similar to Windows and can be read similarly.

TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 12345

The process 12345 is running on port 8080.

Here are the two solutions to solve the java.net.BindException: Address already in use: Bind exception.

Solution 1: Run the Server on Different Port

One solution to this exception is to run the process on another port number. Once we know if any other process is running on the previous port, we can shift the process to another port.

For example, if you are using Tomcat, here is a step-by-step process to change the port for the server.

  • Open the Tomcat directory and then the conf folder.
  • Edit server.xml in Notepad or any other text editor.
  • Now replace the connector port with a new port number. It will look something like this:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

After changing the port:

<Connector port="8880" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

Once the server is shifted to another port number, the error java.net.BindException: Address already in use: Bind will be solved.

Solution 2: Kill the Previous Process

By killing the process already running on the port, we can run our process on it, and the error java.net.BindException: Address already in use: Bind will be solved.

We can run a command to kill the process, but the commands are also different for different platforms.

Windows:

taskkill /F /PID 12345

Mac OS:

kill -9 12345

Unix/Linux:

kill - 12345

The above command can kill the process on the current port for different platforms. After killing the process, we can run our process on the current port, and the error java.net.BindException: Address already in use: Bind will be solved.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Exception