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("println('Hello World')");
        } catch (ScriptException e) {

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you 🥳

Leave a Reply

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