How To Fix java.net.ConnectException: Connection refused in Java

java.net.ConnectException: Connection refused is a common exception if you are dealing with network operations in Java. This error indicates that you are trying to connect to a remote address and connection is refused remotely. It’s possible if there is no remote process/service listening at the specified address or port.

You might see one of the following error lines in your stack trace when you get Connection refused exception.

  java.net.ConnectException: Connection refused: connect
  at java.net.DualStackPlainSocketImpl.connect0(Native Method)
  at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
  at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337)
  at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198)
  at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
  at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
  at java.net.Socket.connect(Socket.java:579)
  at java.net.Socket.connect(Socket.java:528)
  java.net.ConnectException: Connection refused: connect
  at java.net.PlainSocketImpl.socketConnect(Native Method)
  at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
  at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
  at java.net.Socket.connect(Socket.java:520)
  at java.net.Socket.connect(Socket.java:470)

java.net.ConnectException: Connection refused exception clearly indicates that the program you are executing is trying to connect to server side but a successful connection cannot be established.

Possible Causes of java.net.ConnectException: Connection refused

1. Client is trying to connect wrong IP or port: This is a common reason when you get ConnectException. Sometimes applications running on the server side allocate different ports each time they are started. You can either configure the application on the server side, so that it uses fixed port, or you can change the client application, so that it connects to correct port.

2. Server is not running: This is also a possible reason for java.net.ConnectException.
You should check whether the server is open or not. It might be a database server, an application server or a custom server you have implemented. If you try to open a socket connection to a closed server, you will get a connection exception.

3. Server is open but it is not listening connections: This might be the case, for instance, when application server is up and running but an installed application is not started correctly. You should check admin console of your server to check the status of applications on the server. Sometimes application is started correctly but it isn’t listening the port, you are trying to connect.

4. Firewall is blocking connections to remote host: Firewall might be the problem if you cannot connect to server side. If a firewall is blocking connections to server, client application cannot open connection on the specified IP and port.

How To Resolve java.net.ConnectException: Connection refused exception

1. Check IP address and port: First step to resolve java.net.ConnectException is checking IP address and port. If IP address and port are correct, then you should follow the other steps below.

2. Ping the server IP address: You can use ping tool to test whether you can connect to server or not.

A successful ping response is as follows:

C:\>ping 192.168.1.1

Pinging 192.168.1.1 with 32 bytes of data:
Reply from 192.168.1.1: bytes=32 time=1ms TTL=64
Reply from 192.168.1.1: bytes=32 time=1ms TTL=64
Reply from 192.168.1.1: bytes=32 time=1ms TTL=64
Reply from 192.168.1.1: bytes=32 time=1ms TTL=64

Ping statistics for 192.168.1.1:
     Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
     Minimum = 1ms, Maximum = 1ms, Average = 1ms

Ping response for an unsuccessful connection is as follows:

C:\>ping 192.168.1.23

Pinging 192.168.1.23 with 32 bytes of data:
Reply from 192.168.1.23: Destination host unreachable.
Reply from 192.168.1.23: Destination host unreachable.
Reply from 192.168.1.23: Destination host unreachable.
Reply from 192.168.1.23: Destination host unreachable.

Ping statistics for 192.168.1.23:
     Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

3. Try connecting the server with Telnet: You can use telnet application to simulate connection to the server. To use telnet type telnet {Server IP address} {Server port} on the command prompt. If telnet responds “Could not open connection to the host”, then you should investigate further for problems. Maybe, server is not running, or there might be a firewall issue.

C:\>telnet 127.0.0.1 8080
Connecting To 127.0.0.1...Could not open connection to the host, on port 8080: Connect failed