diff -r bb0a7975b31d src/jdk.management.agent/share/classes/jdk/internal/agent/AgentConfigurationError.java --- a/src/jdk.management.agent/share/classes/jdk/internal/agent/AgentConfigurationError.java Tue Dec 24 09:38:41 2019 +0000 +++ b/src/jdk.management.agent/share/classes/jdk/internal/agent/AgentConfigurationError.java Fri Dec 27 17:44:58 2019 +0000 @@ -55,6 +55,8 @@ "agent.err.invalid.jmxremote.port"; public static final String INVALID_JMXREMOTE_RMI_PORT = "agent.err.invalid.jmxremote.rmi.port"; + public static final String INVALID_JMXLOCAL_PORT = + "agent.err.invalid.jmxlocal.port"; public static final String PASSWORD_FILE_NOT_SET = "agent.err.password.file.notset"; public static final String PASSWORD_FILE_NOT_READABLE = diff -r bb0a7975b31d src/jdk.management.agent/share/classes/sun/management/jmxremote/ConnectorBootstrap.java --- a/src/jdk.management.agent/share/classes/sun/management/jmxremote/ConnectorBootstrap.java Tue Dec 24 09:38:41 2019 +0000 +++ b/src/jdk.management.agent/share/classes/sun/management/jmxremote/ConnectorBootstrap.java Fri Dec 27 17:44:58 2019 +0000 @@ -116,6 +116,8 @@ "com.sun.management.jmxremote.host"; public static final String RMI_PORT = "com.sun.management.jmxremote.rmi.port"; + public static final String LOCAL_PORT = + "com.sun.management.jmxlocal.port"; public static final String CONFIG_FILE_NAME = "com.sun.management.config.file"; public static final String USE_LOCAL_ONLY = @@ -539,9 +541,24 @@ localhost = "127.0.0.1"; } + // User can specify a port to be used to start Local Connector Server, + // if port is not specified random one will be allocated. + int localPort = 0; + String localPortStr = System.getProperty(PropertyNames.LOCAL_PORT); + try { + if (localPortStr != null) { + localPort = Integer.parseInt(localPortStr); + } + } catch (NumberFormatException x) { + throw new AgentConfigurationError(INVALID_JMXLOCAL_PORT, x, localPortStr); + } + if (localPort < 0) { + throw new AgentConfigurationError(INVALID_JMXLOCAL_PORT, localPortStr); + } + MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); try { - JMXServiceURL url = new JMXServiceURL("rmi", localhost, 0); + JMXServiceURL url = new JMXServiceURL("rmi", localhost, localPort); // Do we accept connections from local interfaces only? Properties props = Agent.getManagementProperties(); if (props == null) {