You will find prebuilt js.jar and js-engine.jar in the release tab. You can put these two jars in your CLASSPATH. When creating script engine in your app, you need to use "rhino" as engine name.
Using Rhino jsr-223 engine from Java
import javax.script.*;
public class Main {
public static void main(String[] args) throws Exception {
ScriptEngineManager m = new ScriptEngineManager();
// specifically look for "rhino" engine
ScriptEngine engine = m.getEngineByName("rhino");
System.out.println(engine.eval("33 + 232"));
}
}
compile above using
javac Main.java
run it using
java -cp js-engine.jar:js.jar Main
Java 8 brings Nashorn as new JavaScript implementation for JSR 223 (javax.scripting). While this is certainly great news (Nashorn is way faster than Rhino by directly generating Java code), it comes with some challenges:
Rhino had some extensions and more or less other interpretations on how to combine the Java world with JavaScript. Therefore you cannot simply replace Rhino by Nashorn. One case (which ruined our day) is that you cannot call static methods on instances. Therefore we had to get Rhino up and running in Java 8 until we have our scripts re-written.
When you go to java.net to get scripting project, you will find it closed.
However, there are some mirror on github and this project forked on https://github.com/scijava/javax-scripting
This fork is a mod to build JSR-223 engine with JDK8 guided by the post on openjdk.java.net
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino
- Prerequisites: need
jdk
andant
installed - Move to PROJECT_DIR/engines/javascript-jdk8-rhino/make and run:
ant
- Binaries will be placed on
PROJECT_DIR/engines/javascript-jdk8-rhino/build
- Choosing rhino version:
It was configurated to download rhino from maven with ivy under PROJECT_DIR/engines/javascript-jdk8-rhino/make/ivy.xml
<dependency org="org.mozilla" name="rhino" rev="1.7R4"/>
- Rhino bundled with JDK7 has version 1.7R3: link
<dependency org="org.mozilla" name="rhino" rev="1.7R3"/>
- Rhino bundled with JDK6 has version 1.6R2
<dependency org="rhino" name="rhino" rev="1.6R2"/>