Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import static javax.script.ScriptContext.ENGINE_SCOPE;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleScriptContext;

public class Main {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        engine.eval("var msg = 'Hello globals'");
        engine.eval("print(this.msg);");
        ScriptContext ctx = new SimpleScriptContext();
        ScriptContext defaultCtx = engine.getContext();
        Bindings engineBindings = defaultCtx.getBindings(ENGINE_SCOPE);
        ctx.setBindings(engineBindings, ENGINE_SCOPE);
        engine.eval("print(this.msg);", ctx);
    }
}