Yoodley is reader-supported. When you buy through links on our site, we may earn an affiliate commission.
The simplest remedy is to figure out what is causing java.net.ConnectException: Connection refused. What is most significant in this case is the approach taken in determining the correct cause and solution. Here are some suggestions that may assist you in determining the root cause.
- First, ping the destination host; if the host is ping-able, this indicates that both the client and server machines are connected to the network.
- Use telnet to establish a connection to the server’s host and port. Inability to connect indicates that there is a problem with your client software code. Additionally, by examining the telnet output, you will be able to determine whether the server is functioning or not, as well as whether the server is severing the connection from the client.
Reasons For The Error: java.net.ConnectException: Connection refused
Connection rejected is a clear example of a client attempting to establish a connection on a TCP port but failing. Here are some of the possible causes of the error that could be encountered:
Reason 1: Client and Server, either or both of them are not in the network.
The fact that client and server are not linked to a LAN, the internet, or any other network is a possibility, and in that case, Java will throw an error message.This error message is the same one we are dealing with: java.net.ConnectException: Connection refused.
Reason 2: Server is not running
The second most frequently encountered issue is that the server is not up and operating. In that scenario, you will additionally receive java.net. ConnectException: Connection refused error.
What we don’t like about it is that no matter what the reason for the issue is, it always produces the same error message.
Furthermore, you can use the following networking commands, such as ping to determine whether the server is up and running and listening on the specified port.
Reason 3: The server is running but not listening on the port, a client is trying to connect.
This is yet another typical reason for “java.net.ConnectException: Connection refused “, in which the server is up and running but is listening on a different port than usual. It’s difficult to figure out this case unless you sit down and think about it, then you can verify the settings. If you are working on a large project with a hierarchical configuration file, it is possible that either the default configuration or or another setting is taking precedence over your current configuration.
Reason 4: Firewall is not permitted for host-port combination
Firewalls are used to secure almost every corporate network today. It is necessary to raise firewall requests from both sides when connecting to another company’s network, for example, when starting a FIX session to the broker in an Electronic Trading System, in order to verify that the IP address and port number of the other company’s network are permitted. If the firewall is not allowing the connection, you will also receive the same java.net.ConnectException: Connection refused exception in Java application.
Reason 5: Host Port combination is incorrect.
This could be yet another explanation for the existence of java.net.Connection refused. You should consider the possibility that either you are supplying an invalid host port combination or that a previously used host port combination has been modified on the server’s end. Check for the most recent configuration on both the client and server sides to avoid connection denied exceptions from occurring.
Reason 6: Incorrect protocol in Connection String
TCP is the underlying protocol for several high-level protocols, including HTTP, RMI, and others. TCP is a transmission control protocol. Ensure that you are giving the correct connection string to the server, which begins with the protocol that the server expects, for example, if the server has exposed its service via RMI, the connection string should begin with rmi:/
How to fix java.lang.OutOfMemoryError: Java heap space?
An easy technique to resolve OutOfMemoryError in Java is to increase the maximum heap size using JVM options “-Xmx512M”, which will resolve your OutOfMemoryError quickly.
The second method to resolve OutOfMemoryError in Java is more difficult and occurs when you don’t have much memory and even after increasing the maximum heap size you still get java.lang.OutOfMemoryError; in this situation, you should definitely profile your programme and look for any memory leak.
You may also check your heap dump with Eclipse Memory Analyzer or any profiler such as Netbeans or JProbe. This is a difficult solution that will take some time to investigate and detect memory leaks.
How to solve java.lang.OutOfMemoryError: PermGen space?
As stated in the above paragraph, the OutOfMemory issue in Java occurs when the heap’s Permanent generation is depleted. To resolve this OutOfMemoryError in Java, use the JVM option “-XX: MaxPermSize” to raise the heap size of the Perm area.
You can also provide the initial size of Perm Space with “-XX: PermSize,” and by retaining both the initial and maximum Perm Space, you can avoid certain entire garbage collection that may occur when Perm Space is resized.
java.lang.OutOfMemoryError in Java can be tricky at times, and in those circumstances, profiling is the best answer. While you have the option to increase heap size in Java, it is advised that you follow memory management techniques while coding and add null to any unnecessary references.