Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java script engine


May 10, 2021 Java


Table of contents


Java Script Tutorial - Java Script Engine


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

ScriptEngine interface is the interface in which its instances execute scripts written in scripting language

ScriptEngineFactory performs two tasks:

  • Create an instance of the script engine.
  • Provides information about the script engine, such as engine name, version, language, etc.

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

ScriptEngineManager class discovery and instantiation scripting engine.

The engine that is available

The following code shows how to list all available script engines.

import java.util.List;

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 produces the following results. Java script engine

Example

The following code shows how JavaScript, Groovy, Jython, and JRuby can print messages on standard output.

If the script engine is not available, the program prompts the user for an error message.

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

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 produces the following results.

Java script engine

Find out the syntax

To get the syntax for printing messages on standard output, use the method getOutputStation in the ScriptEngineFactory class.

The following snipper shows how to get Nashorn's syntax.

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

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("\"w3cschool\"");
        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 produces the following results.

Java script engine