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

public static void main(String[] args) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine jsEngine = manager.getEngineByExtension("js");
    jsEngine.eval("println ('Hello! JavaScript executed from a Java program.')");
}

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();
    engine.eval("print(this.msg);", ctx);
}

From source file:InterfaceTest.java

public static void main(String args[]) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("javascript");
    engine.eval("function run() {print('www.java2s.com');}");
    Invocable invokeEngine = (Invocable) engine;
    Runnable runner = invokeEngine.getInterface(Runnable.class);
    Thread t = new Thread(runner);
    t.start();//from   w  w  w.  jav  a 2 s.co m
    t.join();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval("loadWithNewGlobal('res/nashorn4.js')");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval("load('res/nashorn3.js')");
}

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) 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);//from www .j  a v  a2  s.c o m

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

From source file:JavaObjectDemo.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine jsEngine;
    jsEngine = manager.getEngineByExtension("js");
    jsEngine.eval("importPackage(javax.swing);var optionPane =JOptionPane.showMessageDialog(null, 'Hello!);");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xyz = "3*3+3";
    String kkk = "(100 % 6)* 7";
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine se = manager.getEngineByName("JavaScript");
    Object result1 = se.eval(xyz);
    Object result2 = se.eval(kkk);
    System.out.println("result1: " + result1);
    System.out.println("result2: " + result2);
}

From source file:Main.java

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

    Double result = (Double) rhino.eval("1 + 2");
    Integer i = result.intValue();
    System.out.println(i);//from  ww  w. ja  va2 s  .com
}