Example of Nashorn Javascript (js) Engine in Java 8 - Write javascript (js) code and use eval() method to execute it >
import java.io.FileNotFoundException;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Nashorn_HelloWorld {
public static void main(String[] args) throws Exception {
System.out.println("Start");
// Get nashorn javascript engine
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("nashorn");
// Write javascript (js) code and use eval() method to execute it
scriptEngine.eval("var name = 'ankit';"
+ "print('Hello ' + name);");
System.out.println("Finish");
}
}
/* OUTPUT
Start
Hello ankit
Finish
*/
|
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.