How to Fix java.net.ConnectException: Connection refused: connect in Java (original) (raw)

java.net.ConnectException: Connection refused: connect is one of the most common networking exceptions in Java. This error comes when you are working with client-server architecture and trying to make a TCP connection from the client to the server. Though this is not as cryptic as java.lang.OutOfMemoryError: Java heap space or java.lang.UnsupportedClassVersionError, it’s still a frequent problem in distributed Java applications. java.net.ConnectException: Connection refused: connect also comes in the case of RMI (Remote Method Invocation) because RMI also uses TCP-IP protocol underneath. While writing client socket code in Java, You should always provide proper handling of this exception.

In this Java tutorial, we will see why connection refused exception comes and how to solve java.net.ConnectException: Connection refused: connect Exception in Java. Normally, Java books like Head First Java won't teach you much about how to deal with such exceptions, it's simply too much to ask for a beginner's book.

java.net.ConnectException: Connection refused Error – Possible reasons

java.net.ConnectException: Connection refused: connect solution fixConnection refused is a clear case of a client trying to connect on a TCP port but not being able to succeed. Here are some of the possible reasons why java.net.ConnectException: Connection refused: connect comes:

1) Client and Server, either or both of them are not in the network.

Yes it's possible that they are not connected to LAN or internet or any other network, in that case, Java will throw

"java.net.ConnectException: Connection refused" exception on client side.

2) Server is not running

The second most common reason is the server is down and not running. In that case, also you will get java.net.ConnectException: Connection refused error. What I don't like is that the message it gives, no matter what is the reason it prints the same error.

By the way, you can use the following networking commands e.g. ping to check if the server is running and listening on the port.

3) The server is running but not listening on the port, a client is trying to connect.

This is another common cause of "java.net.ConnectException: Connection refused", where the server is running but listening on a different port. It’s hard to figure out this case, until, you think about it and verify the configuration.

If you are working on a large project and have a hierarchical configuration file, Its possible that either default configuration is taking place or some other settings are overriding your correct setting.

4) Firewall is not permitted for host-port combination

Almost every corporate network is protected by firewalls. If you are connecting to some other company network e.g. opening a FIX session to the broker, in an Electronic Trading System, then you need to raise firewall requests from both sides to ensure that they permit each other's IP address and port number.

If the firewall is not allowing connection then also you will receive the same java.net.ConnectException: Connection refused exception in Java application.

5) Host Port combination is incorrect.

This could be another reason for java.net.ConnectException: Connection refused: connect. It’s quite possible that either you are providing an incorrect host port combination or an earlier host port combination has been changed on the server-side. Check the latest configuration on both client and server-side to avoid connection refused exception.

6) Incorrect protocol in Connection String

TCP is the underlying protocol for many high-level protocols including HTTP, RMI, and others. While passing connection string, you need to ensure that you are passing the correct protocol, which the server is expecting e.g. if the server has exposed its service via RMI then the connection string should begin with rmi://

If you are curious to learn more and don't mind researching much, then checking on some networking and Java performance courses to start with.

How to Fix java.net.ConnectException: Connection refused: connect in Java

How to solve java.net.ConnectException: Connection refused

The simple solution is to find out the actual reason of java.net.ConnectException: Connection refused, Possibly one of the reasons mentioned above, and solve that. What is most important here is the approach of finding correct cause and solution. Here are some tips which may help you to identify real cause of java.net.ConnectException: Connection refused:

  1. First try to ping the destination host, if the host is ping-able it means the client and server machine are in the network.

  2. Try connecting to server host and port using telnet. If you are able to connect means something is wrong with your client code. Also, by observing telnet output you will come to know whether the server is running or not or the server is disconnecting the connection.

That’s all on What is java.net.ConnectException: Connection refused: connect error in Java and how to fix it. As I said there is no definite reason for this error and could be many things that can go wrong. Thankfully, this is more to do with configuration or environment and once you find out the real cause, you might not even need to make any change on your side.

Related Java debugging tutorials for Java programmers

And lastly one question for you? When was the last time you saw "java.net.ConnectException: Connection refused" error in Java and how did you fixed it? Was it because your server was down or are you using wrong URL/port number combination, or your server was too busy processing other request or deadlocked?