本地JMX调用因ClassCastException而失败

时间:2015-04-14 17:24:56

标签: java jmx

我正在尝试使用下面的简单代码在本地调用JMX服务。它适用于JConsole,但是在命令行下它会在粘贴代码的最后一行抛出异常。

    String serviceURL = "service:jmx:rmi:///jndi/rmi://localhost:" + configuration.getJmxPort();
    String[] credentials = new String[]{configuration.getUsername(), configuration.getPassword()};
    Map<String, String[]> attributes = new HashMap<String, String[]>();
    attributes.put("jmx.remote.credentials", credentials);

    JMXServiceURL jmxUrl = new JMXServiceURL(serviceURL);
    jmxCon = JMXConnectorFactory.connect(jmxUrl, attributes);

堆栈跟踪如下所示:

java.lang.ClassCastException: com.sun.jndi.rmi.registry.RegistryContext cannot be cast to javax.management.remote.rmi.RMIServer
    at javax.management.remote.rmi.RMIConnector.narrowJRMPServer(RMIConnector.java:1897)
    at javax.management.remote.rmi.RMIConnector.findRMIServerJNDI(RMIConnector.java:1892)
    at javax.management.remote.rmi.RMIConnector.findRMIServer(RMIConnector.java:1856)
    at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:257)
    at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:248)
    at com.aaa.aaa.aaa.jmx.AaaJmxClient.main(AaaJmxClient.java:36)

1 个答案:

答案 0 :(得分:4)

我认为JMX服务网址应为 -

String serviceURL = "service:jmx:rmi:///jndi/rmi://localhost:" + configuration.getJmxPort() + "/jmxrmi";