5

For a java application when jmx is enabled and the port is set 0, a port will be dynamically allocated:

-Dcom.sun.management.jmxremote.port=0

What is the best way to find out what port is allocated? I managed to find it out using ps and pfiles on Solaris, hoping there is a simpler why to find it (programmatically)

Also is there a better way to assign dynamic jmx ports to java applications on the same box and keep track of them?

Gray
  • 115,027
  • 24
  • 293
  • 354
user518066
  • 1,277
  • 3
  • 23
  • 35
  • Are you talking about remotely or locally? I don't think there is a way to know remotely which port was allocated. Are you talking from within the JVM or on the same OS but from another program? – Gray Aug 29 '13 at 14:54

1 Answers1

1

This question was answered here

String url = sun.management.ConnectorAddressLink.importRemoteFrom(0)
        .get("sun.management.JMXConnectorServer.0.remoteAddress");
String portStr = url.substring(url.lastIndexOf(":") + 1, url.lastIndexOf("/jmxrmi"));      
int port = Integer.valueOf(portStr);
System.out.println(port);
Mikita Harbacheuski
  • 2,193
  • 8
  • 16