Disclosure: This article may contain affiliate links. When you purchase, we may earn a small commission.

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

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.


Mohanrajurallabandi said...

at com.mysql.jdbc.NonRegisteringDriver.connect ...why we get this exception and how can we resolve it..can anyone tell me please

Anonymous said...

java.net.ConnectException: Connection refused: connect , is pretty general error. Real error is always behind, I suggest looking top level error and caused by clause. This will give you more ideas. In simple words, this error just says that your client and server is not able to connect, but doesn't tell anything about why connection is not happening. There could be thousands of reason of a client and server not connecting including environment issues, host settings, network settings and application settings itself.

Anonymous said...

Hello there, I am getting following Connection refused: connect exception while trying to send an email from localhost. I am using smpt port 25

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1984)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:656)
at javax.mail.Service.connect(Service.java:345)
at javax.mail.Service.connect(Service.java:226)
at javax.mail.Service.connect(Service.java:175)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at Testing.main(Testing.java:62)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:301)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950)
... 7 more
Please suggest what is wrong?

Unknown said...

I am also facing this type of problem.......please suggest me
Exception in thread "main" org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.ConnectException: Connection refused. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.ConnectException: Connection refused; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.ConnectException: Connection refused
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1227)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:322)
at javax.mail.Service.connect(Service.java:236)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:306)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296)
at com.aa.meals.mail.MealsMailSenderBean.sendMail(MealsMailSenderBean.java:21)
at com.aa.common.log.TestMail.main(TestMail.java:14)

Chadpanther said...

help my java is not allowing me to install it says the installer cannot proceed with the current internet settings

tirupati said...

following is my java code error.
Exception in thread "main" java.net.UnknownHostException: ubaid.tk
could you help me.

Anonymous said...

cd /etc/postfix/

cp –p mail.cf mail.cf.orig

vi mail.cf


#inet_interfaces = all

#inet_interfaces = $myhostname

#inet_interfaces = $myhostname, localhost

inet_interfaces = localhost


inet_interfaces = all

#inet_interfaces = $myhostname

#inet_interfaces = $myhostname, localhost

#inet_interfaces = localhost

postfix stop

postfix start

Test connection from unix prompt

telnet pslab17 25

Trying 1xx.1xx.2xx.xx...

Connected to pslab17.

Escape character is '^]'.

220 pslab17.localdomain ESMTP Postfix


221 2.0.0 Bye

Connection closed by foreign host.

Anonymous said...

google "setting java options -D ipv4=true"

Unknown said...

Please check for the port number as its already in use by another application , if yes, kill that process and restart the server.like
go to command prompt-->type netstat -ano | find "port number"
you will find list of applications using your port number.. then just type
taskkill /pid "pid of port no" /f

restart server then it should work..

Anonymous said...

I am getting java.net.SocketException: Network is down: listen failed exception
what may be the reason?

Arvind said...

Hostname maps to multiple IP addresses and one of the IP address is down. Some network libraries automatically try a connection to second server while others need to have code in the application. nslookup hostname. $telnet hostname 25 needs to be replaced with $telnet ip-address 25

Anonymous said...

An important point to note is that if you are using alias e.g. DNS to connect to database or servers then you must remember that JVM can cache DNS value to avoid looking up again and again when a connection needs to be made. This is all ok and improves performance but also risky in the even to fail-over when DNS points to another server. There is a JVM option to configure this behavior e.g. how long to cache, cache or not etc, worth looking.

Unknown said...

please help.

java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:388)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:523)
at sun.net.www.http.HttpClient.(HttpClient.java:227)
at sun.net.www.http.HttpClient.New(HttpClient.java:300)
at sun.net.www.http.HttpClient.New(HttpClient.java:317)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
at java.net.URL.openStream(URL.java:1010)
at com.lowagie.text.Image.getInstance(Unknown Source)
at com.lowagie.text.Image.getInstance(Unknown Source)
at jsp_servlet._health_ecard.__hlth_idcard._jspService(__hlth_idcard.java:217)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:301)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:184)
at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:526)
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:253)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1078)
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:396)
at com.allianz.weo.struts.WeoRequestProcessor.processForwardConfig(WeoRequestProcessor.java:141)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:232)
at com.allianz.weo.struts.WeoRequestProcessor.process(WeoRequestProcessor.java:37)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:301)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:184)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3729)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3696)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2273)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1490)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)

Unknown said...

org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost

javin paul said...

@Unknown, check if your server or Java application is running or port is correct.

Lokeshg.gujjar@gmail.com said...

My javanet connection on scratch day

javin paul said...

hello Lokesh, what does that mean? are you getting any error?

Anonymous said...

Connection refused: connect:
Connection refused: connect
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.freedomgroup.common.exception.TFGExceptionUtility.createThrowable(TFGExceptionUtility.java:328)
at com.freedomgroup.common.exception.TFGExceptionUtility.createSessionException(TFGExceptionUtility.java:239)
at com.freedomgroup.common.gui.request.TFGInvocationHandler.invokeService(TFGInvocationHandler.java:97)
at com.freedomgroup.common.gui.request.TFGInvocationHandler.invokeService(TFGInvocationHandler.java:239)
at com.freedomgroup.common.gui.request.TFGInvocationHandler.invoke(TFGInvocationHandler.java:157)
at com.sun.proxy.$Proxy0.getSystemDeploymentName(Unknown Source)
at com.freedomgroup.policystar.gui.util.TFGPolicyStarUtilities.getSystemDeploymentName(TFGPolicyStarUtilities.java:2460)
at com.freedomgroup.policystar.gui.base.TFGPolicyStarGUIRunner.main(TFGPolicyStarGUIRunner.java:425)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:606)
at java.net.Socket.connect(Socket.java:555)
at java.net.Socket.(Socket.java:451)
at java.net.Socket.(Socket.java:303)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.open(MultiThreadedHttpConnectionManager.java:1361)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at com.freedomgroup.common.communicator.TFGHttpClient.processRequest(TFGHttpClient.java:227)
at com.freedomgroup.common.communicator.TFGHttpRequestHandler.processRequest(TFGHttpRequestHandler.java:99)
at com.freedomgroup.common.gui.request.TFGInvocationHandler.invokeService(TFGInvocationHandler.java:79)
... 5 more

javin paul said...

How can I help? did you checked if the server is up and running and whether you can connect to the server? If you are running your from home then make sure you check that VPN is connected to your office network as well.

Post a Comment