tombrown52
tombrown52

Reputation: 492

Using Mozilla Rhino JSR223 with Java8

I wish to allow my project to support custom script code, both Javascript and Python. To easily support multiple languages, I want to use the ScriptEngine interface (defined by the JSR-223 specification). This leads to the problem: Java 6 and 7 use the Mozilla Rhino javascript engine and Java 8 uses the new Nashorn javascript engine. Unfortunately there are several incompatibilities between Rhino and Nashorn.

The project must run on both Java 7 and 8. Since I want the scripts to behave the same no matter which JVM is used, I thought it might be best to bundle Mozilla Rhino with my application, but I am unable to find a JSR-223 (ScriptEngine) wrapper for it.

Is there such a wrapper for the stock Mozilla Rhino?

I could settle for a backport of Nashorn that is compatible with Java 7.

Would I be better served by switching to Apache BSF instead of JSR-223?

Upvotes: 2

Views: 2921

Answers (2)

Guizhou Feng
Guizhou Feng

Reputation: 121

The current available solutions on the web like below two are based on build locally instead of based on system like Maven, and the solution is much easier to get work based on Maven repository, just add below dependency, it will bring in org.mozilla:rhino automatically

<dependency>
    <groupId>cat.inspiracio</groupId>
    <artifactId>rhino-js-engine</artifactId>
    <version>1.7.7.1</version>
<dependency>

Using Rhino with Java 8

Using Rhino JSR-223 engine with JDK8

Upvotes: 3

tombrown52
tombrown52

Reputation: 492

Apache BSF does not appear to have active development, so I ruled it out.

I was not able to find a backport of Nashorn for Java 7.

I was able to use the JSR-223 wrapper from here: https://java.net/projects/scripting/sources/svn/show/trunk/engines/javascript

By separating the RhinoScriptEngine implementation (and related classes) into it's own Maven project, I was able to create an artifact that includes both the JSR-223 wrapper and any specific version of Rhino that is needed.

Upvotes: 1

Related Questions