How to find the default JMX port number?

207,467

Now I need to connect that application from my local computer, but I don't know the JMX port number of the remote computer. Where can I find it? Or, must I restart that application with some VM parameters to specify the port number?

By default JMX does not publish on a port unless you specify the arguments from this page: How to activate JMX...

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9010
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=localhost

NOTE: you need to be careful of the security ramifications of some of the above settings.

Also, if you are running you should be able to access any of those system properties to see if they have been set:

if (System.getProperty("com.sun.management.jmxremote") == null) {
    System.out.println("JMX remote is disabled");
} else [
    String portString = System.getProperty("com.sun.management.jmxremote.port");
    if (portString != null) {
        System.out.println("JMX running on port "
            + Integer.parseInt(portString));
    }
}

Depending on how the server is connected, you might also have to specify the following parameter. As part of the initial JMX connection, jconsole connects up to the RMI port to determine which port the JMX server is running on. When you initially start up a JMX enabled application, it looks its own hostname to determine what address to return in that initial RMI transaction. If your hostname is not in /etc/hosts or if it is set to an incorrect interface address then you can override it with the following:

-Djava.rmi.server.hostname=<IP address>

As an aside, my SimpleJMX package allows you to define both the JMX server and the RMI port or set them both to the same port. The above port defined with com.sun.management.jmxremote.port is actually the RMI port. This tells the client what port the JMX server is running on.

Share:
207,467
chance
Author by

chance

programmer

Updated on July 09, 2022

Comments

  • chance
    chance almost 2 years

    I am running a Java application on Java 6 VM on a remote Windows XP, on which I can run jvisualvm.exe to connect to the running application automatically.

    Now I need to connect that application from my local computer, but I don't know the JMX port number of the remote computer. Where can I find it? Or, must I restart that application with some VM parameters to specify the port number?

    After reading the question How to find the JMX port in a server, I executed the command on the remote computer

    netstat -apn
    

    but got nothing.

  • Claudio
    Claudio almost 8 years
    You missed -Dcom.sun.management.jmxremote.ssl=false and -Djava.rmi.server.hostname=<IP address>
  • Gray
    Gray almost 8 years
    Neither of those are required @Claudio up to Java 7 at least.
  • Claudio
    Claudio almost 8 years
    Not in my experience. I'm running Java 7 on Linux CentOS 6 . I found your answer, and it didn't work. Fortunately, you also provided the link to the original answer and adding those flags it started working.No idea which one was actually needed and why.
  • Koekiebox
    Koekiebox about 7 years
    If you are using Wildfly, please consult; docs.jboss.org/author/display/WFLY10/…
  • Dragas
    Dragas over 2 years
    You can always enable it even after the JVM is started using jcmd ManagementAgent.start, provided that you have access to the process
  • beaudet
    beaudet over 2 years
    Probably obvious, but you need to remove the "#" and comments that follow from the VM arguments or you might get an error about class "#" not being found :)
  • beaudet
    beaudet over 2 years
    You also might need to add these two additional arguments: -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost
  • Gray
    Gray over 2 years
    Thanks @beaudet. Done.