Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

The Tomcat java.net.BindException: Address Already in Use Error in Java

$
0
0

1. Overview

In this quick tutorial, we’ll look at what causes the common java.net.BindingException Error: Address already in Use error and how we can deal with it.

2. When Does the Error Occur?

As we know, the Apache Tomcat server, by default, uses the 8080 port.

Port numbers range from 0 to 65535, however, a port can only be occupied by a single application at any time.

The exception states that the application is trying to use a port that’s already taken by some other process, or we didn’t stop the Tomcat server properly.

3. Diagnosing

To solve this error, we can either kill the service taking that port or can change our web server to run on another port.

3.1. Discovering the Conflict

In this case, we need to find out which application is using the port.

The netstat command can be used for discovering current TCP/IP connections.

Below are the commands that can be used to find and kill the process in different environments.

On Windows, the last column of output will give us the process id of the service currently running on 8080:

netstat -ano | find "8080"

Output:

TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 21376

Here, 21376 is the process id of the process which is listening on port 8080.

On Unix/Linux Environment:

netstat -pant | grep "8080"

Output:

TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 21376

Same as Windows output. Here, 21376 is the process id of the process which is listening on port 8080.

On Mac OS X:

lsof -t -i :8080

Output:

21376

It will display the PID only.

3.2. Running Server on Another Port

If we know what process is running, why it’s running and that it needs to be running on that port, we can change the port that our server application is attempting to run on.

To change the Tomcat port, we need to edit the server.xml file. To do this:

  • Open tomcat/conf folder
  • edit server.xml
  • replace the connector port with the new port
  • restart tomcat server

The server.xml file looks like this:

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

Now Tomcat will run on the customized port.

3.3. Killing the Running Service

To stop the running process, we can use the kill command. 

Using the process ID that we found in 3.1., we’ll require different commands depending on the Operating System we’re running.

On Windows environment:

taskkill /F /PID 21376

On Unix/Linux environment:

kill - 21376

Mac OS X environment:

kill -9 21376

4. Conclusion

As mentioned at the beginning of the article, java.net.BindingException is a prevalent but easily resolved error.

The main difficulty is in finding the conflicting service using the port with the netstat terminal application then deciding the appropriate course of action.

Once discovered, the fix is easy.


Viewing all articles
Browse latest Browse all 4535

Trending Articles