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("JavaScript");
    String script = "printf('%d + %d = %d', 1, 2, 1 + 2);";
    engine.eval(script);//from w w w.j  a  v a  2  s  .  co  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 = "print('Hello', 'World!');";
    engine.eval(script);//w  w w .  jav a 2s.  c  om
    script = "print('Hello World!');";
    engine.eval(script);
}

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(msg.toUpperCase());print(msg.indexOf('a'))";
    engine.put("msg", "java2s.com");
    engine.eval(script);// w  w  w .j  a v a  2 s .c o m
}

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 w ww.ja  va  2s .  c  om
}

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 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 ww  .  j av a2 s .  c  o m
    t.join();
}

From source file:Main.java

public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    String script = "print ('www.java2s.com')";
    try {/* www . j  a  va2 s . com*/
        engine.eval(script);
    } catch (ScriptException e) {
        e.printStackTrace();
    }
}

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);//from w  w w  . j  ava  2 s  .  c  o  m
    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");

    if (engine instanceof Compilable) {
        System.out.println("Script compilation is supported.");
    } else {//from  w w  w. j  a  v a 2  s  .co m
        System.out.println("Script compilation is not supported.");
    }

}