JavaScriptEngineInfo.java - JavaScript Engine Details

Q

How to get JavaScript Engine detailed information from the SJP API?

✍: FYIcenter

A

If you want to verify the JavaScript Engine detailed information, you can try the following example program:

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

public class JavaScriptEngineInfo {
   public static void main(String[] args) throws Exception {
      ScriptEngineManager m = new ScriptEngineManager();
      ScriptEngine e = m.getEngineByName("JavaScript");
      System.out.println("\nFrom JavaScript:");
      e.eval("print('   Hello world!')");
	  
	  ScriptEngineFactory f = e.getFactory();
      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 the example with Java SE 7 JDK compiler:

>\fyicenter\jdk-1.7.0\bin\javac JavaScriptEngineInfo.java

>\fyicenter\jdk-1.7.0\bin\java JavaScriptEngineInfo

From JavaScript:
   Hello world!

JavaScript engine info:
   Engine name: Mozilla Rhino
   Engine version: 1.7 release 3 PRERELEASE
   Language name: ECMAScript
   Language version: 1.8
   Engine class: com.sun.script.javascript.RhinoScriptEngine
   Factory class: com.sun.script.javascript.RhinoScriptEngineFactory
   Manager class: javax.script.ScriptEngineManager

The output tells you the Rhino JavaScript library is the default JavaScript engine in Java SE 7.

 

SJP (Scripting for the Java Platform) API

⇒⇒FAQ for Rhino JavaScript Java Library

2017-07-25, 1005👍, 0💬