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) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    try {//from w  ww .  j  a  v a 2s . c om
        FileReader reader = new FileReader("yourFile.js");
        engine.eval(reader);
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:CompilableDemo.java

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

    Compilable jsCompile = (Compilable) engine;
    CompiledScript script = jsCompile.compile("function hi () {print ('www.java2s.com !'); }; hi ();");

    for (int i = 0; i < 5; i++) {
        script.eval();//from   w  w w.j av  a 2  s  .c om
    }
}

From source file:Main.java

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

From source file:Main.java

public static void main(String args[]) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("javascript");
    try {/*from  w ww  . j av a2s  .  co  m*/
        engine.put("name", "abcde");
        engine.eval("var output = '';for (i = 0; i <= name.length; i++) {"
                + "  output = name.charAt(i)+'-' + output" + "}");
        String name = (String) engine.get("output");
        System.out.println(name);
    } catch (ScriptException e) {
        System.err.println(e);
    }
}

From source file:Main.java

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

    String script = "print(msg)";
    try {//from ww w. ja  v a  2  s .com
        engine.put("msg", "Hello from Java program");
        engine.eval(script);
    } catch (ScriptException e) {
        e.printStackTrace();
    }
}

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", 2);
    try {//from  w ww.j  ava2 s . com
        String expression = "(a + b) > 2";
        Object result = engine.eval(expression);
        System.out.println(expression + " ? " + result);
    } catch (ScriptException se) {
        se.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    try {//  ww w.j a v  a2 s  .  c o  m

        ScriptEngineManager sem = new ScriptEngineManager();
        ScriptEngine se = sem.getEngineByName("JavaScript");
        String myExpression = "('abc' == 'xyz' && 'thy' == 'thy') || ('ujy' == 'ujy')";
        System.out.println(se.eval(myExpression));

    } catch (ScriptException e) {

        System.out.println("Invalid Expression");
        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

    engine.setContext(ctx);//from   w  w  w. j  a  v  a2s. co m
}

From source file:Main.java

public static void main(String[] args) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    Main result = new Main();
    engine.put("result", result);
    String script = "3 + 4; result.setValue(1);";
    engine.eval(script);//from   w w  w. jav a2s.  co m
    int returnedValue = result.getValue();
    System.out.println("Returned value is " + returnedValue);
}

From source file:Main.java

public static void main(String[] args) {
    String scriptFileName = "c:/test.js";
    Path scriptPath = Paths.get(scriptFileName);
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    try {//from  w  ww. j  a v  a 2 s.c  o m
        Reader scriptReader = Files.newBufferedReader(scriptPath);
        engine.eval(scriptReader);
    } catch (IOException | ScriptException e) {
        e.printStackTrace();
    }
}