As @RealSkeptic noted, the built-in script mechanism of OpenJDK 7 ( Rhino 1.7 r4
) has no problems with the javascript fragment running above. It seems that Rhino 1.7 r3
it cannot start it, so its launch using Oracle Java 7 requires external Rhino 1.7 r4
(or higher), which can be downloaded from here . For completeness only, the java equivalent of the code in the question, based on the Rhino native API, looks like this:
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;
public class Rhino {
public static void main(String[] args) throws Exception {
Context context = Context.enter();
try {
ScriptableObject scope = context.initStandardObjects();
context.evaluateString(scope, "var char = 'a'", "test", 1, null);
} finally {
Context.exit();
}
}
}
Note that import declarations are important, because the same classes are available in the JDK in a different package:
import sun.org.mozilla.javascript.internal.Context;
import sun.org.mozilla.javascript.internal.ScriptableObject;
Importing them leads to using the built-in engine with the Rhino API, which will not work.