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) 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);
        }/*  ww  w . ja  va2s.  com*/
    }
    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) throws Exception {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval("load('res/nashorn5.js')");

    Invocable invocable = (Invocable) engine;

    Product product = new Product();
    product.setName("Rubber");
    product.setPrice(1.99);/*from w w w  . j a va  2  s . c  o  m*/
    product.setStock(1037);

    Object result = invocable.invokeFunction("getValueOfGoods", product);
    System.out.println(result);
}

From source file:Main.java

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

From source file:InvocableTest.java

public static void main(String args[]) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("javascript");
    try {//from   w w w .ja va  2s  .com
        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 ScriptException, NoSuchMethodException {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval("function foo(predicate, obj) { return !!(eval(predicate)); };");

    Invocable invocable = (Invocable) engine;

    Person person = new Person();
    person.setName("Hans");

    String predicate = "obj.getLengthOfName() >= 4";
    Object result = invocable.invokeFunction("foo", predicate, person);
    System.out.println(result);/*w  ww  .jav  a 2  s .  c o  m*/
}

From source file:Main.java

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

    String script = "var year = 2015";

    engine.eval(script);
    Object year = engine.get("year");
    System.out.println("year's class:" + year.getClass().getName());
    System.out.println("year's value:" + year);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    String script = "print('Hello', 'World!');";
    engine.eval(script);
    script = "print('Hello World!');";
    engine.eval(script);//from  ww w  .  j  a v a 2s  .com
}

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  v a2  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:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    String script = "printf('%d + %d = %d', 1, 2, 1 + 2);";
    engine.eval(script);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    // create a script engine manager
    ScriptEngineManager factory = new ScriptEngineManager();
    // create a JavaScript engine
    ScriptEngine engine = factory.getEngineByName("JavaScript");
    // evaluate JavaScript code from String
    Object obj = engine.eval("1+2");
    System.out.println(obj);/*from  w ww.  j  a va2  s  .co m*/
}