I'm trying to invoke JMX service locally using the following simple code below. It works fine with JConsole, however under command line it throws exception on the last line of the pasted code.
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);
The stack trace looks like this:
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)