Easily modifying and rebuilding JMX source code from Java SE 6 source bundle

If you are interested in modifying, recompiling and using the JMX source code part of Java SE 6 (aka Mustang), but do not want to bother with setting up the build of the whole Mustang sources, this "Hands On Lab" is for you. It will show you how to easily take advantage of the Ant's build.xml file provided with JMX sources to achieve just this. The level of this lab is beginner to intermediate.

Requirements for this lab

  • Netbeans is not required, you just need a text editor

  • a Mustang source bundle snapshot, for working into it.
    You can download the latest snapshot of Mustang sources from https://mustang.dev.java.net/

  • Mustang SDK binaries for compiling and running the example.
    You can download the latest snapshot of Mustang binaries also from https://mustang.dev.java.net/

  • Ant 1.5 or higher.
    You can get Ant from http://ant.apache.org/


Overview of this lab

  1. Environment configuration: add Ant and Java to your PATH environment variable

  2. Build JMX classes with the JMX build.xml provided in Mustang source bundle.
    No modifications to code yet.
    Check the jmx.jar has been created.

  3. Compile and Run a simple example that prints the JMX implementation name.
    Make use of the -Xbootclasspath/p: option so that the built JMX classes take precedence over the platform's.

  4. Edit the JMX ServiceName.java file and change the JMX implementation name.
    Rebuild JMX classes.

  5. Re-run the example, which should print the modified implementation name value.

  6. Optionally, also run the same example with the modified and rebuilt jmx.jar in the classpath, but not in bootclasspath prepend, to verify that the modified version value is not printed because the JMX classes of the platform take precedence.


The lab in details

NOTE: All along this document, you will encounter paths beginning like the three paths below. Of course, these paths will need to be adapted to your actual paths on your system, which should be obvious to figure out:


1. Setup environment

Check the requirements for this lab listed above and make sure you have available on your system:

  • a copy of a recent Mustang source snapshot to work into
  • a recent Mustang SDK binaries snapshot
  • a distribution of Ant 1.5 or higher

Add Ant and the Mustang binaries to your PATH environment variable, and then check this is correct, eg:

$ export PATH=/home/asmith/ant/1.6.5/bin:/home/asmith/mustang_snapshot/b77_bin/jdk1.6.0/bin:${PATH}

$ ant -version
Apache Ant version 1.6.5 compiled on June 2 2005

$ java -version
java version "1.6.0-beta2"
Java(TM) SE Runtime Environment (build 1.6.0-beta2-b77)
Java HotSpot(TM) Server VM (build 1.6.0-beta2-b77, mixed mode)


2. Build the JMX classes as is

cd into the j2se subdir of the directory where you have your extracted copy of the Mustang source snapshot. Checks its contents are for now similar to what is shown below. After we have performed the build, a build_jmx subdir will appear here.

$ cd  /home/asmith/mustang_snapshot/b77_src/j2se

$ ls -l
drwxrwxr-x  14 asmith  staff         512 Mar 30 17:17 make/
drwxrwxr-x   6 asmith  staff         512 Mar 30 17:19 src/

Note that we will ignore the contents of the make subdir for this lab.
cd into JMX sources subdir and checks the JMX build.xml file is there.
$ cd src/share/classes/javax/management

$ ls -l build.xml
-rw-rw-r--   1 asmith  staff        9132 Mar 30 17:18 build.xml

  If you want, you can see available targets for this project by typing: ant -projecthelp. Now, simply launch the build of JMX classes by typing:
$ ant

  After the build has completed, you can check the JMX jar file is available, and also check the information put in the MANIFEST of the JMX jar file:
$ cd ../../../../../build_jmx/lib

$ ls -l 
-rw-rw-r--   1 asmith  staff     8351568 Mar 30 17:52 jmx.jar

$ unzip -p jmx.jar  META-INF/MANIFEST.MF
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.6.0-beta2-b77 (Sun Microsystems Inc.)
Build-JDK: 1.6.0-beta2-b77
Build-Platform: sparc SunOS 5.10
Build-User: asmith

Name: common
Sealed: true
Specification-Title: JMX(TM) API
Specification-Version: 1.3
Specification-Vendor: Sun Microsystems, Inc.
Implementation-Title: JMX(TM) API, Java SE 6 implementation
Implementation-Version: 2006.03.30_17:51:45_MEST rebuild of Mustang JMX sources
Implementation-Vendor: Source bundle from Sun Microsystems, Inc. - Customer rebuilt


3. Compile and Run a simple example that prints the JMX version

cd up to the j2se directory, the one containing the build_jmx subdir, and create an example_jmx subdir into which to write and build our simple JMX agent:

$ cd ../../

$ pwd

$ mkdir example_jmx

  cd into the example_jmx subdir:
$ cd example_jmx

  Create the Agent.java file by copying the code below and saving it into a file named Agent.java:
/\* Simple JMX Agent which prints the JMX implementation name \*/

import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.MBeanServerDelegate;

public class Agent {

    public static void main(String[] args) throws Exception {
	MBeanServer server = ManagementFactory.getPlatformMBeanServer();

	String implName = (String) server.getAttribute(

	System.out.println("JMX Implementation Name = " + implName );

Compile the Agent.java file:
$ javac Agent.java

Check the compiled Agent.class file is there.
Now, there is a little tricky part for running the agent. We need to tell the Java VM to prepend its bootclasspath with the built jmx.jar, otherwise the JMX classes already in the Mustang binaries would take precedence over the built ones:
$ java -Xbootclasspath/p:/home/asmith/mustang_snapshot/b77_src/j2se/build_jmx/lib/jmx.jar  Agent
JMX Implementation Name = JMX


4. Edit the JMX ServiceName.java file and rebuild JMX classes

cd into the JMX source directory containing the ServiceName.java file:

$ cd ../src/share/classes/com/sun/jmx/defaults/

Edit the ServiceName.java file and change the value of the JMX_IMPL_NAME string. Let's modify it so that the line reads, say:

    public static final String JMX_IMPL_NAME = "JMX_JavaOne";

  cd back to the directory containing the JMX build.xml file and rebuild all JMX classes:
$ cd ../../../../javax/management/

$ ant all

NOTE: it is important above to clean the previously built classes (which the target "all" does) before building them again as the JMX_IMPL_NAME is a static field and the javac compiler inlines its value in the compiled ServiceName class and in all the classes using it.


5. Re-run the example

cd back into the example_jmx subdir and run again the simple JMX Agent:

$ cd ../../../../../example_jmx/

$ java -Xbootclasspath/p:/home/asmith/mustang_snapshot/b77_src/j2se/build_jmx/lib/jmx.jar  Agent
JMX Implementation Name = JMX_JavaOne

NOTE: In the command above, make sure the path to your freshly rebuilt jmx.jar which you put after -Xbootclasspath/p: is correct, otherwise you will see the JMX Implementation Name value unchanged!
Why? See the next and last step...


6. Run the same example without the bootclasspath prepend option

If the path to your freshly rebuilt jmx.jar file is incorrect, or if you simply put it in your classpath instead of prepending your bootclasspath with it, the JMX classes already in the Mustang platform binaries are loaded in priority, and therefore you do not see your changes. Try just putting your modified jmx.jar in your classpath:

$ java -cp .:/home/asmith/mustang_snapshot/b77_src/j2se/build_jmx/lib/jmx.jar  Agent
JMX Implementation Name = JMX


6. The end

This is the end of this lab.
I hope you enjoyed it, learned usefull tips for you to reuse, and will now play for yourself with the JMX code.

For more JMX examples and tutorials, you should check Daniel's blog articles, starting with: Looking for JMX Overview, Examples, Tutorial, and more?

Joël Féraud


It is quite likely that you will get a OutOfMemory error when you try to build. If you are running Linux/Unix first increase the JVM heap space with
export ANT_OPTS=-Xmx512m
and it should work.

Posted by Lars Westergren on mai 26, 2006 at 05:31 AM CEST #

If you get the error
[javac] javac: invalid source release: 1.6
from ant when doing the test build, then you have exported
but you must also remember to do
export JAVA_HOME=/home/yourname/jdk1.6.0

Posted by Lars Westergren on juin 25, 2006 at 10:53 AM CEST #


Posted by guest on octobre 05, 2006 at 02:10 PM CEST #

can u plz send me JMX api docs Thanks Karthik

Posted by Kathik on décembre 29, 2006 at 08:22 AM CET #


Posted by guest on mars 26, 2007 at 10:05 AM CEST #

Hi, I'm John, send me an email.

Posted by John Burris on juin 14, 2007 at 07:34 AM CEST #


Please send me some code to get the details about
the session & threads and print it on screen or write to a file which is show into jconsole.

Posted by Vikash on mai 03, 2008 at 03:08 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed



« avril 2014