A BeanShell Plugin for JConsole

... A few weeks back A. Sundararajan was blogging about a script shell plugin for jconsole - which made it possible to use JavaScript to get at the remote JMX instrumentation. Well, what about using BeanShell?...

Quoting from www.beanshell.org:

BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. BeanShell dynamically executes standard Java syntax and extends it with common scripting conveniences such as loose types, commands, and method closures like those in Perl and JavaScript.

Since I am not a JavaScript Guru, being able to script directly in plain Java is something that strongly appealed to me - so I decided to give it a try.

In a further article Groovier jconsole! Sundar explains that the script shell plugin makes it possible to use any language for which you have a JSR-223 script engine.
As it appears, the BeanShell JSR-223 script engine is on its way - but is not yet part of the latest BeanShell stable release (BeanShell-2.0b4 as I am writing this article).

Does this means that we cannot use BeanShell with jconsole yet?

Well as it turns out - BeanShell comes with its own swing component - unfortunately also called JConsole :-( - and that component can be very easily embedded in jconsole using the jconsole Plugin API. So I started by taking example on the JTop demo, and less than one hour later, I was able to issue my first BeanShell command:

for (o : server.queryNames(null,null) ) { print(o); }

which lists the MBeans registered in the remote MBeanServer to which jconsole is connected.

BeanShell plugin tab in jconsole

To achieve this I had only to write two small classes: BeanShellPlugin and BeanShellPanel. This was disconcertingly easy. The BeanShellPanel class creates the BeanShell interpreter and its swing component. The BeanShellPlugin creates the BeanShellPanel and also makes the JConsoleContext available in the BeanShell interpreter, in a variable called jcontext. The MBeanServerConnection to the remote MBeanServer to which jconsole is connected is also made available in the BeanShell interpreter in a variable called server. So when you call

for (o : server.queryNames(null,null) ) { print(o); }

in the beanshell interpreter, you actually list the MBeans registered in the MBeanServer of the remote VM.

I then had to set up a META-INF directory as explained in JConsolePlugin:

Setting up a META-INF directory in NetBeans IDE

... and that's it! I just let NetBeans IDE build the whole stuff, then cd'd to the BeanShellPlugin project directory, and invoked

jconsole -pluginpath dist/BeanShellPlugin.jar

That's magic!

Want to try it out? Here is how to proceed:

  • Get the BeanShell-2.0b4 at www.beanshell.org.
  • Get my BeanShellPlugin NetBeans project (Java Sources, ~13 k)
  • Unjar the BeanShellPlugin-src.jar, and load the BeanShellPlugin project into NetBeans IDE
  • Within NetBeans IDE Library Manager, create a library called BeanShell-2.0b4 in which you will put bsh-2.0b4.jar, and add the library to the BeanShellPlugin project.
  • Adds the JDK 6 jconsole.jar and tools.jar to the project libraries

Adding jars and libraries to the BeanShellPlugin project in NetBeans IDE

  • Now build the BeanShellPlugin.jar by invoking Clean And Build Project on the BeanShellPlugin project
  • And finally, cd to the BeanShellPlugin project directory and launch jconsole:
  • jconsole -pluginpath dist/BeanShellPlugin.jar

If you don't want to use NetBeans it will be a little harder: you will have to build the BeanShellPlugin.jar by hand, and ensure that it includes a META-INF/services/com.sun.tools.jconsole.JConsolePlugin file containing the line beanshellplugin.BeanShellPlugin. You will also need to invoke jconsole with the options:

jconsole -J-Djava.class.path=tools.jar:jconsole.jar:bsh-2.0b4.jar -pluginpath BeanShellPlugin.jar

I hope you will enjoy jconsole and BeanShell!

Note You will find more on JSR-223 and BeanShell at scripting.dev.java.net and on Pat Niemeyer's Blog
Update: There is now a JSR-223 ScriptEngine for BeanShell. See Sundar's article.
-- daniel


well you hava done some kind of differ things which is rear for java students. best of luck.

Posted by ravi joshi on September 16, 2006 at 12:53 AM CEST #

I am getting "NoClassDefFoundError: bsh/ConsoleInterface" error when I try to run JConsole with this plugin. Any idea why?

Posted by Shahrier Emon on January 08, 2008 at 10:20 PM CET #

Hi Shahrier,

It looks as if JConsole doesn't find bsh-2.0b4.jar.
You might want to check that you haven't mispelled the location of that jar in the option -J-Djava.class.path.

Here are a few things that come to my mind:

Note that you must give the path to each jar when specifying this option.
Also if you're on a windows machine the syntax will be a bit different - on windows the path separator is ';', not ':'.

-- daniel

Posted by daniel on January 09, 2008 at 02:33 AM CET #

Post a Comment:
Comments are closed for this entry.

Daniel Fuchs blogs on Scene Builder, JMX, SNMP, Java, etc...

The views expressed on this blog are those of the author and do not necessarily reflect the views of Oracle.


« July 2016