Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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();
        }
    }
}