Search code examples
javaeclipsedebuggingtomcatremote-debugging

Shutting down remotely debugged tomcat started from eclipse


When I start my Tomcat 7 from Eclipse, I usually add something like

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8101

to its launch configuration, since I sometimes want to attach the Eclipse debugger remotely later. But when I do this and try to shut down the Tomcat from Cclipses "servers" view, I receive the error

FATAL ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT(197)
ERROR: transport error 202: bind failed: Address already in use
ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)
JDWP exit error AGENT_ERROR_TRANSPORT_INIT(197): No transports initialized 

I assume this is because Eclipse starts a new process that normally tells the running Tomcat to shut down, and also uses the debugging arguments I introduced in to the launch configuration. This process fails, since the named port 8101 is already used. Does anybody have an idea to allow a clean Tomcat shutdown in that setting?

(I do not want to start the Tomcat in Debug mode in the first place since that slows down both eclipse and the Tomcat. Neither do I want to restart it in debug mode when I want to debug, since that takes quite some time.)


Solution

  • In Tomcat's server.xml

    <Server port="8005" shutdown="SHUTDOWN">

    The setting can be used to shutdown Tomcat. You can write a simple program and run it.

    import java.net.*;
    public class t {
        public static void main(String[] args) throws Exception {
            Socket s = new Socket("127.0.0.1",8005);
            s.getOutputStream().write("SHUTDOWN".getBytes());
            s.close();
        }
    }