List of usage examples for javax.script ScriptEngine eval
public Object eval(Reader reader) throws ScriptException;
eval(String)
except that the source of the script is provided as a Reader
From source file:FuncEvaluator.java
public static void main(String[] args) { if (args.length != 2) { System.err/*from w w w . j a v a 2s. c o m*/ .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) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); String script = "print ('www.java2s.com')"; try {//w w w.j a v a 2s .co m engine.eval(script); } catch (ScriptException e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String args[]) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("javascript"); try {// w w w .ja v a 2 s.c o m engine.put("name", "abcde"); engine.eval("var output = '';for (i = 0; i <= name.length; i++) {" + " output = name.charAt(i)+'-' + output" + "}"); String name = (String) engine.get("output"); System.out.println(name); } catch (ScriptException e) { System.err.println(e); } }
From source file:RunJavaScript.java
public static void main(String args[]) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("javascript"); try {/*from w w w . j a va 2s. com*/ 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"); try {//from ww w . ja v a 2 s .c o m FileReader reader = new FileReader("yourFile.js"); engine.eval(reader); reader.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { String scriptFileName = "c:/test.js"; Path scriptPath = Paths.get(scriptFileName); ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); try {// w w w. j a v a2 s. com Reader scriptReader = Files.newBufferedReader(scriptPath); engine.eval(scriptReader); } catch (IOException | ScriptException e) { e.printStackTrace(); } }
From source file:ScriptExecutionReaderDemo.java
public static void main(String[] args) throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine jsEengine = manager.getEngineByExtension("js"); Reader reader = new InputStreamReader(new FileInputStream("yourJavaScript.js")); jsEengine.eval(reader); }
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); }
From source file:Main.java
public static void main(String[] args) { try {/*from w w w.ja v a2s.com*/ ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine se = sem.getEngineByName("JavaScript"); String myExpression = "('abc' == 'xyz' && 'thy' == 'thy') || ('ujy' == 'ujy')"; System.out.println(se.eval(myExpression)); } catch (ScriptException e) { System.out.println("Invalid Expression"); e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { // Get the Nashorn engine ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); String script = "print(msg)"; try {/*from w w w . ja v a 2 s . c o m*/ engine.put("msg", "Hello from Java program"); engine.eval(script); } catch (ScriptException e) { e.printStackTrace(); } }