script

Get script engine’s details

In this example we shall show you how to get the ScriptEngine‘s details. The ScriptEngine interface provides methods for basic scripting functionality. Applications written to this simple interface are expected to work with minimal modifications in every implementation. It includes methods that execute scripts, and ones that set and get values. To get the ScriptEngine’s details one should perform the following steps:

  • Create a new ScriptEngineManager. The ScriptEngineManager implements a discovery and instantiation mechanism for ScriptEngine classes and also maintains a collection of key/value pairs storing state shared by all engines created by the Manager.
  • Use getEngineFactories() API method to get a list whose elements are instances of all the ScriptEngineFactory classes found by the discovery mechanism.
  • For every ScriptEngineFactory use getEngineName() method to get the full name of the ScriptEngine.
  • Use getEngineVersion() method to get the version of the ScriptEngine.
  • Use getLanguageName() method to get the name of the scripting langauge supported by this ScriptEngine.
  • Use getLanguageVersion() method to get the version of the scripting language supported by this ScriptEngine.
  • Use getExtensions() method to get an immutable list of filename extensions, which generally identify scripts written in the language supported by this ScriptEngine.
  • Use getNames() method to get an immutable list of short names for the ScriptEngine, which may be used to identify the ScriptEngine by the ScriptEngineManager,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngineFactory;
import java.util.List;

public class GetScriptEngine {
    public static void main(String[] args) {

    	// Get new instance of script engine

  ScriptEngineManager manager = new ScriptEngineManager();

  List<ScriptEngineFactory> factories = manager.getEngineFactories();

  // Print details

  for (ScriptEngineFactory factory : factories) {

System.out.println(

  "EngineName
= " + factory.getEngineName());

System.out.println(

  "EngineVersion   = " + factory.getEngineVersion());

System.out.println(

  "LanguageName    = " + factory.getLanguageName());

System.out.println(

  "LanguageVersion = " + factory.getLanguageVersion());

System.out.println(

  "Extensions
= " + factory.getExtensions());

List<String> names = factory.getNames();

for (String name : names) {

    System.out.println("Engine Alias = " + name);

}

  }
    }
}

Output:

EngineName = Mozilla Rhino EngineVersion = 1.7 release 3 PRERELEASE LanguageName = ECMAScript LanguageVersion = 1.8 Extensions =  Engine Alias = js Engine Alias = rhino Engine Alias = JavaScript Engine Alias = javascript Engine Alias = ECMAScript Engine Alias = ecmascript

 
This was an example of how to get the ScriptEngine’s details in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button