Example usage for javax.script ScriptEngine eval

List of usage examples for javax.script ScriptEngine eval

Introduction

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

Prototype

public Object eval(Reader reader) throws ScriptException;

Source Link

Document

Same as eval(String) except that the source of the script is provided as a Reader

Usage

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 {// w w  w .  jav  a  2s  .c o  m
        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) 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);
    int returnedValue = result.getValue();
    System.out.println("Returned value is " + returnedValue);
}

From source file:Main.java

public static void main(String[] args) {
    // Create a script engine manager
    ScriptEngineManager manager = new ScriptEngineManager();

    // Obtain a script engine from the manager
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    // Store the script in a String
    String script = "print('hi from java2s.com!')";
    try {// w  ww . j a  va  2s.  co  m
        // Execute the script
        engine.eval(script);
    } catch (ScriptException e) {
        e.printStackTrace();
    }
}

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:TestCompilationSpeed.java

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

    String fact = "function fact (n){if (n == 0)return 1; else return n*fact (n-1);};";

    long time = System.currentTimeMillis();
    for (int i = 0; i < MAX_ITERATIONS; i++)
        engine.eval(fact);
    System.out.println(System.currentTimeMillis() - time);

    Compilable compilable = null;
    if (engine instanceof Compilable) {
        compilable = (Compilable) engine;
        CompiledScript script = compilable.compile(fact);

        time = System.currentTimeMillis();
        for (int i = 0; i < MAX_ITERATIONS; i++)
            script.eval();//  ww w  .ja v a2  s.  com
        System.out.println(System.currentTimeMillis() - time);
    }
}

From source file:Main.java

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

    manager.put("n1", 1);
    String script = "var sum = n1 + n2;print(msg + " + "' n1=' + n1 + ', n2=' + n2 + " + "', sum=' + sum);";

    engine.put("n2", 2);
    engine.put("msg", "a string");
    engine.eval(script);

    Bindings bindings = engine.createBindings();
    bindings.put("n2", 3);
    bindings.put("msg", "another string");
    engine.eval(script, bindings);/* w w  w  .  ja  v a 2  s . c om*/

    ScriptContext ctx = new SimpleScriptContext();
    Bindings ctxGlobalBindings = engine.createBindings();
    ctx.setBindings(ctxGlobalBindings, GLOBAL_SCOPE);
    ctx.setAttribute("n1", 4, GLOBAL_SCOPE);
    ctx.setAttribute("n2", 5, ENGINE_SCOPE);
    ctx.setAttribute("msg", "ScriptContext:", ENGINE_SCOPE);
    engine.eval(script, ctx);

    engine.eval(script);
}

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 {//from  ww w  .j av a2s.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 Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    if (!(engine instanceof Invocable)) {
        System.out.println("Invoking methods is not supported.");
        return;//  ww w. ja  v  a2s.  c  o m
    }
    Invocable inv = (Invocable) engine;
    String scriptPath = "c:/Java_Dev/calculator.js";

    engine.eval("load('" + scriptPath + "')");

    Object calculator = engine.get("calculator");

    int x = 3;
    int y = 4;
    Object addResult = inv.invokeMethod(calculator, "add", x, y);
    Object subResult = inv.invokeMethod(calculator, "subtract", x, y);
    Object mulResult = inv.invokeMethod(calculator, "multiply", x, y);
    Object divResult = inv.invokeMethod(calculator, "divide", x, y);

    System.out.println(addResult);
    System.out.println(subResult);
    System.out.println(mulResult);
    System.out.println(divResult);
}

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 Invocable)) {
        System.out.println("Interface implementation in script" + "is not supported.");
        return;/*from  w  w  w .  j a  v a 2  s.c o  m*/
    }
    Invocable inv = (Invocable) engine;
    String scriptPath = "c:/Java_Dev/cal.js";
    engine.eval("load('" + scriptPath + "')");
    Calculator calc = inv.getInterface(Calculator.class);
    if (calc == null) {
        System.err.println("Calculator interface " + "implementation not found.");
        return;
    }
    double x = 2.0;
    double y = 1.0;
    double addResult = calc.add(x, y);

    System.out.println(addResult);
}

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