How do I evaluate or execute a script file?

This example demonstrate how to evaluate script that stored on a file. As we know that the eval() method can also accept a Reader object we can then read the script file using FileReader and pass it as the parameter to the eval() method of the ScriptEngine for further evaluation.

package org.kodejava.example.script;

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.io.File;
import java.io.Reader;
import java.io.FileReader;
import java.io.FileNotFoundException;

public class EvalScriptFile {
    public static void main(String[] args) {
        //
        // Obtaining ECMAScript / JavaScript ScriptEngine.
        //
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("ECMAScript");

        try {
            //
            // Create an instance of File object that point to our
            // scripting file. An create a FileReader to read the
            // file to be passed to the ScriptEngine.eval() method.
            //
            // The file need to be placed in the same folder with
            // our class so it enable to read it. We can define the
            // full path to the file also to make easier for the
            // Reader to find it.
            //
            File script = new File("helloworld.js");
            Reader reader = new FileReader(script);

            engine.eval(reader);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }
}

How do I get the supported scripting engine?

package org.kodejava.example.script;

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

public class GetSupportedScriptingEngine {
    public static void main(String[] args) {
        //
        // Creating an instance of ScriptEngineManager an get the list
        // of available ScriptEngineFactory.
        //
        ScriptEngineManager manager = new ScriptEngineManager();
        List factories = manager.getEngineFactories();

        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 names = factory.getNames();
            for (String name : names) {
                System.out.println("Engine Alias = " + name);
            }
        }
    }
}

The code above produces the following information.

EngineName      = Mozilla Rhino
EngineVersion   = 1.6 release 2
LanguageName    = ECMAScript
LanguageVersion = 1.6
Extensions      = [js]
Engine Alias = js
Engine Alias = rhino
Engine Alias = JavaScript
Engine Alias = javascript
Engine Alias = ECMAScript
Engine Alias = ecmascript

Currently, as you can see in the result of the program above, Mozilla Rhino is the only scripting engine provided in the JDK 6 libraries. You can also see that the factory provides many engine name aliases that help you to obtain an engine for the JavaScript programming language.

How do I get ScriptEngine by engine name?

This example shows how to get an instance of ScriptEngine by the engine name. Below we are trying to obtain the JavaScript ScriptEngine.

package org.kodejava.example.script;

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

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

        //
        // Obtain an instance of ScriptEngine using the engine name. For
        // example we get a JavaScript ScriptEngine instance.
        //
        ScriptEngine engine = manager.getEngineByName("JavaScript");

        try {
            engine.eval("print('Hello World');");
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }
}

How do I evaluate a simple script?

The code below will evaluate a simple script that produces a Hello World message. The evaluate the script, the ScriptEngine provides us with some overloaded eval() method, that for instance accept the script in string or in Reader object.

package org.kodejava.example.script;

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

public class EvaluatingScript {
    public static void main(String[] args) {
        //
        // Obtaining JavaScript' ScriptEngine from the ScriptManager
        // using the engine extension.
        //
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine js = manager.getEngineByExtension("js");

        try {
            //
            // Evaluating a simple script
            //
            js.eval("print('Hello World')");
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }
}

How do I create a ScriptEngine for JavaScript?

This examples demonstrate how to create a JavaScript interpreter or the ScriptEngine for running JavaScript script. The classes related to scripting is packaged under the javax.script package.

Java is since version 6 allows us to script Java using a scripting language such as JavaScript. This allows us to let the user of our program to change or add behavior to the program through scripting language.

package org.kodejava.example.script;

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

public class GettingJavaScriptEngine {
    public static void main(String[] args) {
        //
        // Creating an instance of ScriptManager. With ScriptManager in hand we
        // can create an interpreter or ScriptEngine to run JavaScript.
        //
        ScriptEngineManager manager = new ScriptEngineManager();
        
        //
        // Calling manager.getEngineByExtension("js") returns a ScriptEngine
        // implementation for JavaScript. "js" is the standar extension for
        // JavaScript script file.
        //
        ScriptEngine engine = manager.getEngineByExtension("js");

        //
        // Do something with the script engine.
        //
    }
}