This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

Debug Note < How do I find the default JMX port number? >

Ask questions

I am running a Java application on a Java 6 VM on remote Windows XP and can run JVisualvm.exe on it to automatically connect to the running application.

Now, I need to connect to the application from the local machine, but I don’t know the JMX port number for the remote machine. Where can I find it?

Or, does the application have to be restarted with some VM parameters to specify a port number?

After looking at the problem “How to find JMX ports in the server”, I executed the command on the remote computer:

netstat -apn
Copy the code

But nothing.

Answer:

Now, I need to connect to the application from the local machine, but I don’t know the JMX port number for the remote machine.

Where can I find it?

Or, does the application have to be restarted with some VM parameters to specify a port number?

By default, JMX will not be published on ports unless you specify parameters on this page: How to activate JMX…

-Dcom.sun.management.jmxremote # no longer required for JDK6
-Dcom.sun.management.jmxremote.port=9010
-Dcom.sun.management.jmxremote.local.only=false # careful with security implications
-Dcom.sun.management.jmxremote.authenticate=false # careful with security implications
Copy the code

If you are running, you should be able to access any of these system properties to see if they are 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)); }}Copy the code

Depending on how the server is connected, you may also have to specify the following parameters.

As part of the initial JMX connection, JConsole connects to the RMI port to determine on which port the JMX server is running. When the JMX-enabled application is initially started, it looks up its host name to determine the address returned in this initial RMI transaction. If your host name is not in /etc/hosts or you set it to the wrong interface address, you can override it with the following command:

-Djava.rmi.server.hostname=<IP address>
Copy the code

By the way, my SimpleJMX package allows you to define a JMX server and RMI port, or set them both to the same port. Defined above com. Sun. Management. Jmxremote. The port of the port is, in fact, RMI port. This tells the client which port the JMX server is running on.

The article translated from Stack Overflow: stackoverflow.com/questions/1…