- 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 "" system property with the "-D" option on the "java" command.

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

 * Copyright (c) 2005 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:

herong> javac

herong> java PrimeNumberSeeker

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

Now my application 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.

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

 java - The Java Program Launcher

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 jpackage - Binary Package Builder

 javadoc - The Java Document Generator

 jdeps - The Java Class Dependency Analyzer

 jdeprscan - The Java Deprecated API Scanner

 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 - JMX Agent for Local Connection

 jconsole - Connecting to a Local JMX Agent - JMX Agent for Remote Connection

 jconsole - Connecting to a Remote JMX Agent

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jhsdb - The Java HotSpot Debugger

 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

 jrunscript - Script Code Shell

 Miscellaneous Tools

 native2ascii - Native-to-ASCII Encoding Converter

 JAB (Java Access Bridge) for Windows

 Archived Tutorials


 Full Version in PDF/EPUB