Nashorn is the JavaScript engine provided with the Java 8. It allows developers to embed JavaScript code inside their Java applications.
Working with Nashorn command-line
The command for running the JavaScript from the command line is jjs.To use jjs we have to add the path to jjs.In Windows, we have to add $JAVA_HOME/bin path to environment variable. In Linux, we have to write following commands in a terminal
$ cd /usr/bin
$ ln -s $JAVA_HOME/bin/jjs jjs
After all set if we give jjs command we get a jjs prompt.
To check the version
To check the Nashorn version we use -v command
jjs -v
Working with JavaScript variables
jjs> var hello = “welcome to Nashorn”
jjs> hello
welcome to Nashorn
jjs>
to print a line
jjs> print (“welcome to Nashorn”)
welcome to Nashorn
jjs>
but as we are in JavaScript engine not in a browser environment so we can’t access to object of the document object model of the brewers.
jjs> window.alert ( welcome)
<shell>:1 ReferenceError: “window” is not defined
jjs>
Running JavaScript from Java class
To use Nashorn from inside Java code, we have to create an instance of ScriptEngine from ScriptEngineManager.
package example; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class Demo { public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("nashorn"); } }
We create an object name Manager from ScriptEngineManager an instance script of scriptEngine which access to all scriptEngine built in the java runtime. We use manager.getEngineByName(“nashorn”);method to create the object.
import javax.script.ScriptEngine; package nashornExmp; import javax.script.ScriptEngineManager; public class Demo { public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("nashorn"); String script = "var hello = 'welcome to javascript';" + "hello;"; } }
Now we can write our JavaScript code.
We can write script as string object.
We have to use only single quotation for the JavaScript literals as double quotation can mess with the java code.
import javax.script.ScriptEngine; package nashornExmp; import javax.script.ScriptEngineManager; public class Demo { public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("nashorn"); String script = "var hello = 'welcome to javascript';" + "hello;"; Object result = engine.eval(script); } }
To execute the script we w=use a method of the script engine engine.eval() which give us an exception
We have to use try catch block for the exception
import javax.script.ScriptEngine; package nashornExmp; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class Demo { public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("nashorn"); String script = "var hello = 'welcome to javascript';" + "hello;"; try { Object result = engine.eval(script); System.out.println(result); } catch (ScriptException e) { // TODO Auto-generated catch block System.out.println(“javascript error”); e.printStackTrace(); } } }