Example usage for javax.script ScriptEngineManager getEngineByName

List of usage examples for javax.script ScriptEngineManager getEngineByName

Introduction

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

Prototype

public ScriptEngine getEngineByName(String shortName) 

Source Link

Document

Looks up and creates a ScriptEngine for a given name.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    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 = engine.eval("c = a + b;");
    System.out.println("a + b = " + result);
}

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);//from ww w . jav  a2s  . 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 ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    Object result = null;//  w  w  w .ja va2  s .  co  m
    result = engine.eval("1 + 2;");
    System.out.println(result);
    result = engine.eval("1 + 2; 3 + 4;");
    System.out.println(result);
    result = engine.eval("1 + 2; 3 + 4; var v = 5; v = 6;");
    System.out.println(result);
    result = engine.eval("1 + 2; 3 + 4; var v = 5;");
    System.out.println(result);
    result = engine.eval("print(1 + 2)");
    System.out.println(result);

}

From source file:InvocableTest.java

public static void main(String args[]) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("javascript");
    try {//  w ww  . ja  v  a2 s . c o m
        engine.eval("function myFunction(name){var output = '';"
                + "  for (i = 0; i <= name.length; i++) {output = name.charAt(i)+'-'+ output"
                + "  } return output;}");
        Invocable invokeEngine = (Invocable) engine;
        Object o = invokeEngine.invokeFunction("myFunction", "abcde");
        System.out.println(o);

    } catch (NoSuchMethodException e) {
        System.err.println(e);
    } catch (ScriptException e) {
        System.err.println(e);
    }
}

From source file:Main.java

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

    ScriptEngine engine = manager.getEngineByName("js");

    engine.eval("function add (a, b) {c = a + b; return c; }");
    Invocable jsInvoke = (Invocable) engine;

    Object result1 = jsInvoke.invokeFunction("add", new Object[] { 10, 5 });
    System.out.println(result1);/* w w w .  j  ava2s .  com*/

    Adder adder = jsInvoke.getInterface(Adder.class);
    int result2 = adder.add(10, 5);
    System.out.println(result2);
}

From source file:Main.java

public static void main(String[] args) {
    // Get the script engine factory for Nashorn
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    ScriptEngineFactory factory = engine.getFactory();

    // Get the script
    String script = factory.getOutputStatement("\"java2s\"");
    System.out.println("Syntax: " + script);

    // Evaluate the script
    try {//ww  w .j av a  2  s. com
        engine.eval(script);
    } catch (ScriptException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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

    // Set ctx as the new default context for the engine
    engine.setContext(ctx);/*w ww.j  av  a 2s . c om*/

    ctx.setBindings(manager.getBindings(), ScriptContext.GLOBAL_SCOPE);

    engine.setContext(ctx);
}

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  w  w  w  .  j  a  va  2s. co  m*/
    }
    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: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 {//from  ww  w  . j a  v  a  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) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    Bindings params = engine.createBindings();

    params.put("stringKey", "Hello");
    params.put("valueKey", 2015);

    Object msg = params.get("stringKey");
    Object year = params.get("valueKey");
    System.out.println("stringKey" + msg);
    System.out.println("valueKey = " + year);

    params.remove("valueKey");
    year = params.get("valueKey");

    boolean containsYear = params.containsKey("valueKey");
    System.out.println("valueKey = " + year);
    System.out.println("params contains year = " + containsYear);
}