-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseNashorn.java
More file actions
31 lines (27 loc) · 851 Bytes
/
Copy pathUseNashorn.java
File metadata and controls
31 lines (27 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.sumit.learn.java8;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
/**
* @author sumit
* Nashorn javascript engine newly added in java 8 , before it there was Rhino engine
*
* How to open Nashorn console
* Ans. jjs (command)
*
*/
public class UseNashorn {
public static void main(String[] args) {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("nashorn");
String script = "var welcome = 'Welcome javascript world';"
+ "welcome;";
try {
String result = (String) scriptEngine.eval(script);
System.out.println(result);
} catch (ScriptException e) {
System.out.println("There was an error while executing javascript.");
e.printStackTrace();
}
}
}