Java JMX tutorial - A "Hello world" JMX application

Java JMX FAQ: Can you share a simple Java JMX example?

Here is some sample Java source code for some JMX tests that I created recently. I got a large percentage of this code from Sun's JMX MBean tutorial when I first started working with JMX, and I think it's some decent "Hello World" starter code. The main thing I've done here is (a) clarify what they've written and (b) added a shell script to start the JMX application. (I'm also about to publish some other JMX source code that I know I wrote myself.)

Our JMX MBean interface

First, here's the source code for a Java MBean interface named HelloMBean:

public interface HelloMBean {
    public void setMessage(String message);
    public String getMessage();
    public void sayHello();

A class to implement the JMX MBean interface

Next, here's the source code for a Java class named Hello that implements the HelloMBean interface we just defined:

public class Hello implements HelloMBean {
    private String message = null;

    public Hello() {
        message = "Hello, world";

    public Hello(String message) {
        this.message = message;

    public void setMessage(String message) {
        this.message = message;

    public String getMessage() {
        return message;

    public void sayHello() {

A JMX example class (a JMX service)

Next up, here's the source code for a Java class named SimpleAgent that includes a main method that starts up our JMX service application:


public class SimpleAgent {
    private MBeanServer mbs = null;

    public SimpleAgent() {

        // Get the platform MBeanServer
        mbs = ManagementFactory.getPlatformMBeanServer();

        // Unique identification of MBeans
        Hello helloBean = new Hello();
        ObjectName helloName = null;

        try {
            // Uniquely identify the MBeans and register them with the platform MBeanServer 
            helloName = new ObjectName("FOO:name=HelloBean");
            mbs.registerMBean(helloBean, helloName);
        } catch(Exception e) {

    // Utility method: so that the application continues to run
    private static void waitForEnterPressed() {
        try {
            System.out.println("Press  to continue...");
        } catch (Exception e) {

    public static void main(String argv[]) {
        SimpleAgent agent = new SimpleAgent();
        System.out.println("SimpleAgent is running...");

A script to start our JMX service

And finally here's a Unix/Linux shell script named that I wrote to run this sample JMX application ("JMX service"):


java \ \ \ \

As you can see from that code, my application will listen on port 1617 of whatever computer system it is run on.

To run all of this code, save the source code shown here to the appropriate files (,, and, and, compile them, and then run them with the script.

Access the JMX service with JConsole

Once the application is running you can then connect to it with the jconsole application and view its JMX resources. I don't have any JConsole screenshots here at this time, but in short, JConsole is a GUI application that Sun ships with the latest SDK releases. If you have the SDK, you should also have an application named jconsole, and you use jconsole as a "JMX console" to connect to both local and remote JMX services.

Assuming that my JMX example application is running on port 1617 (as shown in the script above) and on some remote server, you can access this JMX service via jconsole by (a) specifying the IP address of the server this service is running on and (b) that port. No password is needed to access this JMX service.


You actually use JConsole to connect to your JMX applications. As of Java 5 it looks like JConsole comes with the J2SE environment, so if your Java environment is set up right, you should just be able to type


at your command prompt.

JConsole is a GUI application, so once it starts up, you just give it the information you need to connect to your JMX application. If it's a local JMX application (running on the same computer that jconsole is started from), I think it shows up in the initial JConsole Local list. If it's a remote application (on another computer), you have to give JConsole the IP address and port of your JMX server, along with a Username and Password to connect to the app.

I hope that helps. I linked to it above, but here's a good link that provides more information on using JConsole.

Reading that link, I forgot that you can also connect to a local process if you know the process id, so if the process id is 1234, you can connect to it like this:

jconsole 1234


Could you please help me to write an MBean where i can trigger a shell script on a particualr event.


I have a couple of different articles that help to show how to execute a shell script from a Java application:

The first article is much easier, but doesn't work under some circumstances. The code in the second article works properly because it correctly deals with stdin and stdout via threads. It also includes a link to an Apache exec project, which may be the easiest way to go.

Hopefully those tips will be helpful.



can you explain how to run this program in eclipse

Sorry, I don't know how to run this in Eclipse. I wrote it in Eclipse, but always ran it as shown, through a shell script. I know you can run Java commands with command line arguments like that in Eclipse, but I don't do that too often myself.


Nice Article..helped me a lot to understand what is JMX.