com.sun.management.jmxremote - JMX Agent for Local Connection

This section provides a tutorial example on how to turn on the out-of-the-box JMX agent on Sun JVM for monitoring tool 'jconsole' to connect on the local machine.

Since JDK 1.5, JMX has been included in the HotSpot JVM. You can turn on the out-of-the-box JMX agent when you launch the JVM to run your application. This enables "jconsole" to connect to the JMX agent to monitor you Java application local or remotely.

In order to run your Java application and allow "jconsole" to monitor it on the local machine, you need to turn the "com.sun.management.jmxremote" system property with the "-D" option on the "java" command.

To this out, I modified the my PrimeNumberSeeker.java program with a higher ceiling value of 1000000 to let the loop running much longer:

/* PrimeNumberSeeker.java
 * Copyright (c) 2015 HerongYang.com. All Rights Reserved.
 */
public class PrimeNumberSeeker extends Thread {
   private static final int ceiling = 1000000;
   private static final int interval = 1000;
   private static final int delay = 100;
   public int count = 0;
   public int current = 2;
   public int[] primes = null;
   public static void main(String[] a) {
      System.out.println("Period, Current int, # primes");
      PrimeNumberSeeker t = new PrimeNumberSeeker();
      t.start();
      int i = 0;
      while (true) {
         i++;
         System.out.println( i+", "+t.current+", "+t.count);
         try {
            sleep(interval);
         } catch (InterruptedException e) {
            System.out.println("Monitor interrupted.");
         }
      }
   }
   public void run() {
      primes = new int[ceiling];
      while (count < ceiling) {
         current++;
         int j = 2;
         boolean isPrime = true;
         while (j<current/2 && isPrime) {
            isPrime = current % j > 0;
            j++;
         }
         if (isPrime) {
            count++;
            primes[count-1] = current;
         }
         try {
            sleep(delay);
         } catch (InterruptedException e) {
            System.out.println("Runner interrupted.");
         }
      }
   }
}

To run this program with the out-of-the-box JMX agent turned on, I used the following commands:

C:\herong>javac PrimeNumberSeeker.java

C:\herong>java -Dcom.sun.management.jmxremote PrimeNumberSeeker

Period, Current int, # primes
1, 2, 0
2, 10, 4
...

Now my application PrimeNumberSeeker.java is running with the JMX agent turned on for local connection. See the next section on how to run "jconsole" to monitor this application.

Note that since JDK 1.6, JConsole can also connect to any HotSpot JVM using the Attach API on the local machine. In other words, you can connect to a Java application without the JMX agent.

Last update: 2018.

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 java - The Java Program Launcher

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

jconsole - Java Monitoring and Management Console

 JMX Technology and jconsole Tool

 jconsole - Command Options and Connection Window

com.sun.management.jmxremote - JMX Agent for Local Connection

 jconsole - Connecting to a Local JMX Agent

 com.sun.management.jmxremote.port - JMX Agent for Remote Connection

 jconsole - Connecting to a Remote JMX Agent

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

 jshell - Java Language Shell

 native2ascii - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 Full Version in PDF/EPUB