Example usage for javax.script ScriptEngineManager ScriptEngineManager

List of usage examples for javax.script ScriptEngineManager ScriptEngineManager

Introduction

In this page you can find the example usage for javax.script ScriptEngineManager ScriptEngineManager.

Prototype

public ScriptEngineManager() 

Source Link

Document

The effect of calling this constructor is the same as calling ScriptEngineManager(Thread.currentThread().getContextClassLoader()).

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    File outputFile = new File("jsoutput.txt");
    System.out.println("Script output will be written to " + outputFile.getAbsolutePath());

    FileWriter writer = new FileWriter(outputFile);
    ScriptContext defaultCtx = engine.getContext();
    defaultCtx.setWriter(writer);/* w w  w  .  ja  va  2s. c o  m*/
    String script = "print('Hello custom output writer')";
    engine.eval(script);
    writer.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    manager.put("global", "global bindings");

    dumpBindings(manager.getBindings());
    ScriptEngine engine = manager.getEngineByExtension("js");
    engine.put("engine", "engine bindings");

    dumpBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE));

    dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE));

    Bindings bindings = engine.createBindings();
    bindings.put("engine", "overridden engine bindings");
    bindings.put("bindings", bindings);
    engine.eval("app.dumpBindings (bindings);", bindings);

    ScriptEngine engine2 = manager.getEngineByExtension("js");
    engine2.put("engine2", "engine2 bindings");

    dumpBindings(engine2.getBindings(ScriptContext.GLOBAL_SCOPE));
    dumpBindings(engine2.getBindings(ScriptContext.ENGINE_SCOPE));
    dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE));
}

From source file:Main.java

public static void main(String[] args) throws ScriptException, NoSuchMethodException {
    NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval("load('res/nashorn8.js')");

    engine.invokeFunction("evaluate1"); // [object global]
    engine.invokeFunction("evaluate2"); // [object Object]
    engine.invokeFunction("evaluate3", "Foobar"); // Foobar
    engine.invokeFunction("evaluate3", new Person("John", "Doe")); // [object global] <- ???????
}

From source file:Main.java

public static void main(String[] args) throws ScriptException {
    ScriptEngine jse = new ScriptEngineManager().getEngineByName("javascript");
    Bindings bindings = jse.getBindings(ScriptContext.ENGINE_SCOPE);
    Main test = new Main();
    bindings.put("test", test);
    String preFix = "demo";
    jse.eval("test." + preFix + "_test()");
    preFix = "actual";
    jse.eval("test." + preFix + "_test()");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("javascript");

    engine.eval(new InputStreamReader(Main.class.getResourceAsStream("scripting.js")));

    List<String> list1 = (List<String>) engine.get("list1");
    if (list1 != null) {
        for (String s : (List<String>) list1) {
            System.out.println(s);
        }//from   ww w .  j  ava 2  s. c om
    }
    Invocable engineInv = (Invocable) engine;
    Object obj = engine.get("listObject");
    Object list2 = engineInv.invokeMethod(obj, "getList2");
    if (list2 != null) {
        for (String s : (List<String>) list2) {
            System.out.println(s);
        }
    }
}

From source file:Main.java

public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    engine.put("a", 1);
    engine.put("b", 5);

    Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    Object a = bindings.get("a");
    Object b = bindings.get("b");
    System.out.println("a = " + a);
    System.out.println("b = " + b);

    Object result;/* w  w w .  j av  a  2 s.  c om*/
    try {
        result = engine.eval("c = aaaa + bbbb;");
        System.out.println("a + b = " + result);
    } catch (ScriptException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:CompileTest.java

public static void main(String args[]) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("javascript");
    engine.put("counter", 0);
    if (engine instanceof Compilable) {
        Compilable compEngine = (Compilable) engine;
        try {//  w  w  w.j ava 2s. c o m
            CompiledScript script = compEngine
                    .compile("function count(){counter=counter+1;return counter;}; count();");
            System.out.println(script.eval());
            System.out.println(script.eval());
            System.out.println(script.eval());
        } catch (ScriptException e) {
            System.err.println(e);
        }
    } else {
        System.err.println("Engine can't compile code");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    if (!(engine instanceof Compilable)) {
        System.out.println("Script compilation not supported.");
        return;/*ww w.  j a  v  a2 s  .  c o  m*/
    }
    Compilable comp = (Compilable) engine;

    CompiledScript cScript = comp.compile("print(n1 + n2)");

    Bindings scriptParams = engine.createBindings();
    scriptParams.put("n1", 2);
    scriptParams.put("n2", 3);
    cScript.eval(scriptParams);

    scriptParams.put("n1", 9);
    scriptParams.put("n2", 7);
    cScript.eval(scriptParams);
}

From source file:ScriptExecutionReaderDemo.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine jsEengine = manager.getEngineByExtension("js");
    Reader reader = new InputStreamReader(new FileInputStream("yourJavaScript.js"));
    jsEengine.eval(reader);// w  w w  .  j a  v  a  2  s .c  om
}

From source file:Main.java

public static void main(String[] args) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    ScriptContext defaultCtx = engine.getContext();
    // Work with defaultCtx here

    // Create a new context
    ScriptContext ctx = new SimpleScriptContext();
    // Configure ctx here

    engine.setContext(ctx);//from  w w w  .ja  v a 2s. c o  m
}