title: How to detect if JMX agent is running, and using which port?
body: <p>If Java system property <code>com.sun.management.jmxremote</code> is defined at JVM start-up, the JMX agent will be started on port defined in system property <code>com.sun.management.jmxremote.port</code>.</p>
  
  <p>However, there are also techniques to launch the JMX agent / listener at run-time. (See: <a href="http://docs.oracle.com/javase/8/docs/technotes/guides/management/agent.html" rel="nofollow">Monitoring and Management Using JMX Technology</a> , Section: <em>Mimicking Out-of-the-Box Management Using the JMX Remote API</em>)</p>
  
  <p>Is there a reliable way to detect if the JMX agent is running, and can I find the port at run-time?</p>
  
  <p>Related: What happens if run-time code starts two JMX agents on different ports?</p>
  
  <p>Why do I ask?  I want to auto-magically start the JMX agent on an ephemeral port if not already started.</p>