Created: 2008-08-11 18:03
Updated: 2017-12-20 16:56


jmx4r is a JavaScript bridge for JMX

It can be used to write simple JavaScripts to manage remote Java applications (e.g. JBoss, Tomcat) using JMX.


This bridge works only on Java 6 with a JavaScript engine. This is the case by default for Sun's JVM on Linux and Windows.

For Mac OS X, there are additional steps:

  • Download JSR 223’s engines
  • Copy jsr223-engines/javascript/build/js-engine.jar to /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/lib/ext/
  • Download Mozilla Rhino 1.7.R1
  • Copy rhino1_7R1/js.jar to /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/lib/ext/


To trigger a garbage collection on a Java application:

First, run a Java application:

jconsole \ \ \ \

Then, create a file "memory.js":

var memory = mbsc.getMBean('java.lang:type=Memory');

print("after gc: " + memory.heapMemoryUsage.get("used"));

Finally, run the script to trigger a GC on the remote Java application:

java -jar build/jmx-js.jar memory.js -p 3000 example/memory.js

This expects the remote Java application (e.g. jconsole) running on localhost to accept remote JMX connection on the port 3000.

How to Build

Retrieve the project:

git clone git://
cd jmx-js

Build the jar

ant jar

How to Write Scripts

An object "mbsc" can be used to retrieve MBean:

var mbean = mbsc.getMBean('java.lang:type=Memory');  


var mbeans = mbsc.getMBeans('java.lang:type=MemoryPool,*');

An object "args" contains all the remaning arguments passed on the command line after removing the file name and the optional host and port. For example, if the script is run with "java -jar jmx-js.jar logging.js -p 3000 FINEST", in the script, args[0] is set to "FINEST".

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more