[tomcat] Apache Tomcat :java.net.ConnectException: Connection refused

while i try to stop tomcat server its giving an error like this.

[root@server classes]# service tomcat restart
Stopping Tomcat service:  Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:       /usr
Apr 17, 2011 10:11:53 PM org.apache.catalina.startup.Catalina stopServer
SEVERE: Catalina.stop: 
java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:310)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:176)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:163)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:381)
    at java.net.Socket.connect(Socket.java:537)
    at java.net.Socket.connect(Socket.java:487)
    at java.net.Socket.<init>(Socket.java:384)
    at java.net.Socket.<init>(Socket.java:198)
    at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:421)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:337)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:415)
                                                           [  OK  ]

Starting Tomcat service: Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:       /usr

Can anyone help me to fix this problem?

This question is related to tomcat

The answer is


There is one more explanation for this Connection Refused problem, that I missed from answers above. In my case I had thread running (quartz scheduler) started from context listener (in contextInitialized), never to be stopped (like in contextDestroyed).

This in turn caused the server port associated to be closed, where as the container kept on running (hence: connection refused on the server port).

I fixed it stopping quartz in contextDestroyed and I didn't experience this issue ever since.

See here for similar issues: Tomcat doesn't stop. How can I debug this?


Depending on your version of Tomcat, this might be a simple problem (bug) with a 0-byte log file. Take a look at /var/log/tomcatX.Y where X.Y is your version you're working with and check if the log file catalina.out is read- and writeable. If it has 0 byte and is not accessible, then simply delete it and re-start Tomcat. This solved the problem for us a few times already.


you can try to stop and start again with :

$ cd /path/apache-tomcat x.x.x/bin

then

$ sh shutdown.sh

when succesfully done the last step you must turn on your tomcat and catalina with command

$ sh startup.sh

I managed to resolve my problem with this way


Was the Tomcat running before you restarted it? Was there any other app listening on this port?

The exception is thrown because there was nobody listening on the command port (see <Server port="..." in $tomcat_home/conf/server.xml).


The meaning of this exception is explained here: https://bz.apache.org/bugzilla/show_bug.cgi?id=27829

Summary: Java dies, Tomcat shut down hook is called, exception is thrown.

So if a firewall prevents the shutdown message from reaching Tomcat, Java will eventually die first (ex during system reboot/shutdown), and the exception will appear.

There are other possibilities.

In my case, my problem had something to do with my initscript (Linux) being incorrectly installed. That implied Java was getting killed by the OS during shutdown/reboot and not as a result of the script. The solution as simple as this:

chkconfig --del initscript
chkconfig --add initscript

Before the fix I had the following in rc.d:

find /etc/rc.d | grep initscript | sort
/etc/rc.d/init.d/initscript
/etc/rc.d/rc2.d/S85initscript
/etc/rc.d/rc3.d/S85initscript
/etc/rc.d/rc4.d/S85initscript
/etc/rc.d/rc5.d/S85initscript

After the fix:

find /etc/rc.d | grep initscript | sort
/etc/rc.d/init.d/initscript
/etc/rc.d/rc0.d/K15initscript
/etc/rc.d/rc1.d/K15initscript
/etc/rc.d/rc2.d/K15initscript
/etc/rc.d/rc3.d/K15initscript
/etc/rc.d/rc4.d/K15initscript
/etc/rc.d/rc5.d/S85initscript
/etc/rc.d/rc6.d/K15initscript

Conclusion: if you get this exception, make sure Tomcat is shutdown properly, not as a result of Java being terminated. Check your firewall, shutdown scripts etc.


Not sure if your issue was fixed and how. But I faced the same issue while trying to make a tomcat instance running.

  • The port was not in use.
  • There was no firewall issue.
  • Tomcat instance was starting up fine.

I changed the custom shutdown script and this issue was fixed. Old Script:-

export CATALINA_HOME=/home/lrsprod/ELA/tomcat6/apache-tomcat-6.0.35 $CATALINA_HOME/bin/catalina.sh stop

Added catalina base to it.

export CATALINA_BASE=/home/lrsprod/ELA/tomcat6/ela_instance export CATALINA_HOME=/home/lrsprod/ELA/tomcat6/apache-tomcat-6.0.35 $CATALINA_HOME/bin/catalina.sh stop

That did the trick.


I'd like to add another reason that "catalina.sh" won't be able to connect to port 8005, apart from a firewall or a change in "server.xml": It takes Tomcat time to start listening on port 8005.

It's easy to reproduce this scenario If you start Tomcat and try to stop it immediately. Wait a few minutes and you'll see that the problem disappears because the port has opened.


I also faced this problem . You can try any of these steps :


I've seen a lot of inadequate answers while trying to figure this one out. General response has been "you are trying to stop something that hasn't started" or "some other program is running on the port you need".

The problem for me turned out to be my firewall. I hadn't even considered this, but port 8005 (the port used for shutdown, thanks mindas), was blocked. I changed it, and now, no more error. Good luck.


Another possible root cause is that your tomcat has not completely started yet.

If you do a ps -ef| grep apache, you would see the server running and if you check the catalina.out, it will show that the server initialized in 123ms - but it might still be deploying the applications in your webapps directory.