'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 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();
      int i = 0;
      while (true) {
         System.out.println( i+", "+t.current+", "+t.count);
         try {
         } catch (InterruptedException e) {
            System.out.println("Monitor interrupted.");
   public void run() {
      primes = new int[ceiling];
      while (count < ceiling) {
         int j = 2;
         boolean isPrime = true;
         while (j<current/2 && isPrime) {
            isPrime = current % j > 0;
         if (isPrime) {
            primes[count-1] = current;
         try {
         } 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:\Progra~1\java\jdk1.8.0\bin\javac PrimeNumberSeeker.java

C:\Progra~1\java\jdk1.8.0\bin\java -Dcom.sun.management.jmxremote

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 6 using the Attach API on the local machine. In other words, you can connect to a Java application without the JMX agent.

Last update: 2015.

Table of Contents

 About This Book

 Java Tools Terminology

 Installing Java 8 on Windows

 'javac' - The Java Program Compiler

 'java' - The Java Program Launcher

 'jdb' - The Java Debugger

'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

 'jar' - The JAR File Tool

 'javap' - The Java Class File Disassembler

 'keytool' - Public Key Certificate Tool

 'native2ascii' - Native-to-ASCII Encoding Converter

 Outdated Tutorials


 PDF Printing Version