List of usage examples for javax.script ScriptEngineManager getEngineByName
public ScriptEngine getEngineByName(String shortName)
ScriptEngine
for a given name. 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 not supported."); return;/* w w w . ja va2 s . co m*/ } Compilable comp = (Compilable) engine; CompiledScript cScript = comp.compile("print(n1 + n2)"); Bindings scriptParams = engine.createBindings(); scriptParams.put("n1", 2); scriptParams.put("n2", 3); cScript.eval(scriptParams); scriptParams.put("n1", 9); scriptParams.put("n2", 7); cScript.eval(scriptParams); }
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 va 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:RunJavaScript.java
public static void main(String args[]) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("javascript"); try {/*from ww w.ja va 2s. c o m*/ Double hour = (Double) engine.eval("var date = new Date();" + "date.getHours();"); String msg; if (hour < 10) { msg = "Good morning"; } else if (hour < 16) { msg = "Good afternoon"; } else if (hour < 20) { msg = "Good evening"; } else { msg = "Good night"; } System.out.println(hour); System.out.println(msg); } catch (ScriptException e) { System.err.println(e); } }
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", 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;/*from w w w . ja v a 2 s . c om*/ try { result = engine.eval("c = aaaa + bbbb;"); System.out.println("a + b = " + result); } catch (ScriptException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
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);//from w w w . ja v a2 s. c om 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(); System.out.println(System.currentTimeMillis() - time); } }
From source file:FuncEvaluator.java
public static void main(String[] args) { if (args.length != 2) { System.err//from w w w . ja va 2s .c om .println("usage: java FuncEvaluator scriptfile " + "script-exp"); return; } ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("rhino"); try { System.out.println(engine.eval(new FileReader(args[0]))); System.out.println(engine.eval(args[1])); } catch (ScriptException se) { System.err.println(se.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } }
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 . j a v a 2 s . com } 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 { // 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);/* w w w. j a va 2 s .c o m*/ }
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);/*from w w w . ja v a 2 s . c o m*/ Bindings bindings = engine.createBindings(); bindings.put("n2", 3); bindings.put("msg", "another string"); engine.eval(script, bindings); 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) { // 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 {/* www. j a v a2s.c o m*/ // Execute the script engine.eval(script); } catch (ScriptException e) { e.printStackTrace(); } }