Wednesday, December 2, 2015

Running Java in a Script

Recently I wanted to invoke a JMX operation in a WSO2 server from a script. I was first thinking of writing a simple Java class with a main method and run it as an executable jar file.

Then I found that we can run a script with the command "jrunscript".

I also found an example of interacting with a JMX MBean from Javascript. However this example didn't work with Java 8.

The reason is that from Java 8, the JavaScript Engine for the JVM is Oracle Nashorn. In Java 7, the JavaScript Engine is based on Mozilla Rhino.

Let's see the difference:

Following script works with Java 7, but not with Java 8


packages = new JavaImporter(java.lang,
                            java.lang.reflect);

with (packages)
{
// create Java String array of 5 elements
var a = Array.newInstance(String, 5);

// Accessing elements and length access is by usual Java syntax
a[0] = "scripting is great!";
print(a.length);
print('\n');
print(a[0]);
print('\n');
}


Following is the output:

$ /usr/lib/jvm/jdk1.7.0_80/bin/jrunscript java7-array-test.js 
5
scripting is great! 
$ /usr/lib/jvm/jdk1.8.0_66/bin/jrunscript java7-array-test.js 
script error in file java7-array-test.js : TypeError: Cannot call undefined in java7-array-test.js at line number 7


Let's write the same script to work in Java 8. I referred the Rhino Migration Guide

var Array = Java.type("java.lang.reflect.Array")
var JString = Java.type("java.lang.String")

  // create Java String array of 5 elements
var a = Array.newInstance(JString.class, 5);

// Accessing elements and length access is by usual Java syntax
a[0] = "scripting is great!";
print(a.length);
print(a[0]);


Following is the output:

$ /usr/lib/jvm/jdk1.7.0_80/bin/jrunscript java7-array-test.js 
5
scripting is great! 
$ /usr/lib/jvm/jdk1.8.0_66/bin/jrunscript java7-array-test.js 
script error in file java7-array-test.js : TypeError: Cannot call undefined in java7-array-test.js at line number 7

$ /usr/lib/jvm/jdk1.8.0_66/bin/jrunscript java8-array-test.js 5 scripting is great! $ /usr/lib/jvm/jdk1.7.0_80/bin/jrunscript java8-array-test.js script error in file java8-array-test.js : sun.org.mozilla.javascript.internal.EvaluatorException: missing name after . operator (java8-array-test.js#5) in java8-array-test.js at line number 5

Note: Java 8 also has another tool named "jjs" to invoke the Nashorn engine. See  Java Platform, Standard Edition Nashorn User's Guide

Following is the complete script I wrote to invoke JMX operations in the Metrics Manager MBean in a WSO2 Server.