title: jmx can't connect to localhost
body: <p>Trying to connect a simple JMX  monitoring. Managed application and monitoring tool are on the same server. When trying to connect an error</p>
  
  <blockquote>
    <p>00:30:55,610 FATAL http-8080-6 SiteListener:makeJmxConnection:99 - 
    java.io.IOException: Failed to retrieve RMIServer stub:
    javax.naming.ServiceUnavailableException [Root exception is
    java.rmi.ConnectException: Connection refused to host: localhost;
    nested exception is:      java.net.ConnectException: Connection refused]
      at javax.management.remote.rmi.RMIConnector.connect(Unknown Source)
      at javax.management.remote.JMXConnectorFactory.connect(Unknown
    Source)   at
    com.m.a.s.SiteListener.makeJmxConnection(SiteListener.java:93)    at
    com.m.a.s.SiteListener.getMBeanConnect(SiteListener.java:73)  at
    com.m.a.s.SiteListener.contextInitialized(SiteListener.java:51)   at
    org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972)
      at
    org.apache.catalina.core.StandardContext.start(StandardContext.java:4467)
      at
    org.apache.catalina.core.StandardContext.reload(StandardContext.java:3228)
      at
    org.apache.catalina.manager.ManagerServlet.reload(ManagerServlet.java:918)
      at
    org.apache.catalina.manager.HTMLManagerServlet.reload(HTMLManagerServlet.java:545)
      at
    org.apache.catalina.manager.HTMLManagerServlet.doGet(HTMLManagerServlet.java:121)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)     at
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)  at
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:558)
      at
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      at
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
      at
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
      at
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Unknown Source) Caused by:
    javax.naming.ServiceUnavailableException [Root exception is
    java.rmi.ConnectException: Connection refused to host: localhost;
    nested exception is:      java.net.ConnectException: Connection refused]
      at com.sun.jndi.rmi.registry.RegistryContext.lookup(Unknown Source)
      at com.sun.jndi.toolkit.url.GenericURLContext.lookup(Unknown Source)
      at javax.naming.InitialContext.lookup(Unknown Source)   at
    javax.management.remote.rmi.RMIConnector.findRMIServerJNDI(Unknown
    Source)   at
    javax.management.remote.rmi.RMIConnector.findRMIServer(Unknown Source)
      ... 26 more Caused by: java.rmi.ConnectException: Connection refused
    to host: localhost; nested exception is:      java.net.ConnectException:
    Connection refused    at
    sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)   at
    sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)     at
    sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)    at
    sun.rmi.server.UnicastRef.newCall(Unknown Source)     at
    sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)     ... 31 more
    Caused by: java.net.ConnectException: Connection refused  at
    java.net.PlainSocketImpl.socketConnect(Native Method)     at
    java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)    at
    java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)     at
    java.net.AbstractPlainSocketImpl.connect(Unknown Source)  at
    java.net.SocksSocketImpl.connect(Unknown Source)  at
    java.net.Socket.connect(Unknown Source)   at
    java.net.Socket.connect(Unknown Source)   at
    java.net.Socket.(Unknown Source)    at
    java.net.Socket.(Unknown Source)    at
    sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown
    Source)   at
    sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown
    Source)   ... 36 more</p>
  </blockquote>
  
  <p>On the server, open access so</p>
  
  <pre><code> private static void startJmxServer() throws MalformedURLException, IOException {
  
          Map&lt;String, Object&gt; props = new HashMap&lt;String, Object&gt;();
          RemouteAuthentificator auth = new RemouteAuthentificator();
          props.put("jmx.remote.authenticator", auth);
  
          JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:22414/MServer");
          MBeanServer mbeanSrv = ManagementFactory.getPlatformMBeanServer();
          JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, props, mbeanSrv);
          server.start();
          log.info("JMX RMI Agent has been bound on address: " + url);
      }
  </code></pre>
  
  <p>In an administrative tool under Tomcat,  trying to connect</p>
  
  <pre><code>static private MBeanServerConnection makeJmxConnection(String objectName)
      {
      // objectName = “MServer:name=Settings”;
          MBeanServerConnection mConnect = null;
          try
          {
              String[] credentials = new String[] {"server", "password"};
              Map&lt;String, String[]&gt; props = new HashMap&lt;String, String[]&gt;();
              props.put("jmx.remote.credentials", credentials);
  
              JMXServiceURL url = new JMXServiceURL(“service:jmx:rmi:///jndi/rmi://localhost:22414/MServer”);
              jmxc = JMXConnectorFactory.connect(url, props);
              mConnect = jmxc.getMBeanServerConnection();
              mbeanName = new ObjectName(objectName);
          }
          catch(Exception e)
          {
              log.fatal("", e);
          }
  
          return mConnect;
      }
  </code></pre>
  
  <p>And tried to connect via external ip. Result is the same.</p>