Tuesday, May 12, 2009

Dealing with java.net.BindException: Address already in use

Depending on the software, we see these errors. The underlying reason is the same.
We see the error if a process tries to listen on address+port thats already in use.

java.net.BindException: Address already in use

Address already in use: make_sock: could not bind to address

Unable to bind socket - Address already in use

Under ideal circumstances, after socket connection is dropped (both ends acknowledge FIN), underlying application would call close() which inturn is handled by OS kernel. Unless the OS kernel releases the concerned network resource, the port cannot be used by another process.

TO fix the issue, get the process ID thats already listening and kill it.

On Windows we can use netstat:
Here I am looking for my weblogic process running on port 7001.
netstat would give the process ID (2056 in this case).

C:\>netstat -o -a -n | findstr :7001
TCP   0.0.0.0:7001   0.0.0.0:0   LISTENING   2056
Go to windows process list and kill the process.

On Unix we can use lsof (need lsof package installed)
Here I am looking for my weblogic server listening on port 7002.
18664 is the PID.

# lsof -i | grep 7002
java   18664   root   47u   IPv4   0x30079bee4e0   0t0   TCP   unixmachine:7002   (LISTEN)

We can get the complete process listing as:
# /usr/ucb/ps auwwx | grep 18664

Kill the process as:
# kill -9 18664

No comments: