"java.net.BindException: Address already in use" when I am already running a service on the same port

user2346809 picture user2346809 · Dec 19, 2013 · Viewed 51.7k times · Source

I have one server socket program, when I run this program I get the following error:

java.net.BindException: Address already in use
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)
    at java.net.ServerSocket.bind(ServerSocket.java:328)
    at java.net.ServerSocket.<init>(ServerSocket.java:194)
    at Server.main(Server.java:20)
Could not listen on port: 5434.

A postgres sql server is already running on this port, which explains the error. My hardware device only sends data to this 5434 port.

What are my options to get around this error?

Answer

neoneye picture neoneye · Dec 23, 2014

Mac specific

An instance of my java program was already running and occupying the port. So when I tried starting a new instance then I got the error. In order to kill the old instance I do the following:

prompt> lsof -i:5434
COMMAND   PID    USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
java    71107 neoneye   68u  IPv6 0x4a0371dea87d2cd3      0t0  TCP *:http-alt (LISTEN)
prompt> kill 71107

btw. I don't recommend stopping postgresql this way.