Scripting in Java Tutorial - Scripting in Java Engine








The Java Scripting API has a number of classes and interfaces. They are in the javax.script package.

The ScriptEngine interface is the interface whose instances execute scripts written in a scripting language.

A ScriptEngineFactory performs two tasks:

  • creates instances of the script engine.
  • provides information about the script engine such as engine name, version, language, and so on.

The AbstractScriptEngine class is an abstract class and provides a partial implementation for the ScriptEngine interface.

The ScriptEngineManager class discovers and instantiates the script engines.





Available Engines

The following code shows how to list All Available Script Engines.

import java.util.List;
//  w w  w . j  a v  a2  s .  c om
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;

public class Main {
  public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();

    // Get the list of all available engines
    List<ScriptEngineFactory> list = manager.getEngineFactories();

    // Print the details of each engine
    for (ScriptEngineFactory f : list) {
      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 Short Names:" + f.getNames());
      System.out.println("Mime Types:" + f.getMimeTypes());
      System.out.println("===");
    }
  }
}

The code above generates the following result.





Example

The following code shows how to print a message on the standard output using JavaScript, Groovy, Jython, and JRuby.

If a script engine is not available, the program alerts the user with error message.

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
// ww w .jav  a2s . c  o  m
public class Main {
  public static void main(String[] args) {
    // Get the script engine manager
    ScriptEngineManager manager = new ScriptEngineManager();

    // Try executing scripts in Nashorn, Groovy, Jython, and JRuby
    execute(manager, "JavaScript", "print('Hello JavaScript')");
    execute(manager, "Groovy", "println('Hello Groovy')");
    execute(manager, "jython", "print 'Hello Jython'");
    execute(manager, "jruby", "puts('Hello JRuby')");
  }

  public static void execute(ScriptEngineManager manager, String engineName,
      String script) {

    ScriptEngine engine = manager.getEngineByName(engineName);
    if (engine == null) {
      System.out.println(engineName + " is not available.");
      return;
    }

    try {
      engine.eval(script);
    } catch (ScriptException e) {
      e.printStackTrace();
    }
  }
}

The code above generates the following result.

Find out the Syntax

To get the syntax used to print a message on the standard output, use the method getOutputStatement(String toDisplay) from ScriptEngineFactory class.

The following snippet of code shows how to get the syntax for Nashorn.

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
/* w  ww.  ja  va  2  s .  c  om*/
public class Main {
  public static void main(String[] args) {
        // Get the script engine factory for Nashorn
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        ScriptEngineFactory factory = engine.getFactory();

        // Get the script
        String script = factory.getOutputStatement("\"java2s\"");
        System.out.println("Syntax: " + script);

        // Evaluate the script
        try {
      engine.eval(script);
    } catch (ScriptException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

The code above generates the following result.