JavaScriptRhinoEngine.java - Select SJP RI Engine: Rhino

Q

How to select the SJP Reference Implementation Script Engine, Mozilla Rhino, to run my JavaScript code? I don't want to use the default JavaScript engine.

✍: FYIcenter

A

If you have the SJP Reference Implementation Script Engine, sjp-1_0-fr-ri.zip, installed, you select it to run your JavaScript code instead of using the default script engine, Oracle Nashorn.

1. Make sure 2 JAR files from the SJP Reference Implementation pacakge, sjp-1_0-fr-ri.zip, are installed properly:

>dir \fyicenter\sjp-1_0-fr-ri

07/27/2006  01:09 PM           701,049 js.jar
10/16/2006  07:00 PM            34,607 script-js.jar

2. Get all available script engine factories:

      ScriptEngineManager m = new ScriptEngineManager();
	  List<ScriptEngineFactory> l = m.getEngineFactories();

3. Loop through the factory list, and select "Mozilla Rhino" engine:

	  ScriptEngineFactory f = null;
      for (ScriptEngineFactory i : l) {
         if (i.getEngineName().equals("Mozilla Rhino")) {
			f = i;
         	break;
		 }
      }

Here is the entire example program, JavaScriptRhinoEngine.java, that shows you how to select a specific script engine to run script code:

// Copyright (c) 2017 FYIcenter.com
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import java.util.*;

public class JavaScriptRhinoEngine {
   public static void main(String[] args) throws Exception {
      ScriptEngineManager m = new ScriptEngineManager();
	  List<ScriptEngineFactory> l = m.getEngineFactories();
	  ScriptEngineFactory f = null;
	  
      System.out.println("\nScript Engines:");
      for (ScriptEngineFactory i : l) {
         System.out.println("   Engine name: "+i.getEngineName());
         if (i.getEngineName().equals("Mozilla Rhino")) {
		    System.out.println("      Selected...");
			f = i;
         	break;
		 }
      }
	  
      ScriptEngine e = f.getScriptEngine();
      System.out.println("\nFrom Mozilla Rhino:");
      e.eval("print('   Hello world!')");

      System.out.println("\nJavaScript engine info:");
      System.out.println("   Engine name: "+f.getEngineName());
      System.out.println("   Engine version: "+f.getEngineVersion());
      System.out.println("   Language name: "+f.getLanguageName());
      System.out.println("   Language version: "+f.getLanguageVersion());
      System.out.println("   Engine class: "+e.getClass().getName());
      System.out.println("   Factory class: "+f.getClass().getName());
      System.out.println("   Manager class: "+m.getClass().getName());
    }
}

Compile and run the example program, JavaScriptRhinoEngine.java, with two JAR files from sjp-1_0-fr-ri.zip:

>\fyicenter\jdk-1.8.0\bin\javac JavaScriptRhinoEngine.java

>\fyicenter\jdk-1.8.0\bin\java 
   -cp .;\fyicenter\sjp-1_0-fr-ri\script-js.jar;\fyicenter\sjp-1_0-fr-ri\js.jar 
   JavaScriptRhinoEngine

Script Engines:
   Engine name: Oracle Nashorn
   Engine name: Mozilla Rhino
      Selected...

From Mozilla Rhino:
   Hello world!
JavaScript engine info:
   Engine name: Mozilla Rhino
   Engine version: 1.6 release 2
   Language name: ECMAScript
   Language version: 1.6
   Engine class: com.sun.script.javascript.RhinoScriptEngine
   Factory class: com.sun.script.javascript.RhinoScriptEngineFactory
   Manager class: javax.script.ScriptEngineManager

 

SJP (Scripting for the Java Platform) API

⇒⇒FAQ for Rhino JavaScript Java Library

2017-07-21, 1056👍, 0💬