With Java 8, Nashorn, a much improved javascript engine is introduced, to replace the existing Rhino. Nashorn provides 2 to 10 times better performance, as it directly compiles the code in memory and passes the bytecode to JVM. Nashorn uses invoke dynamics feature, introduced in Java 7 to improve performance.
jjs
For Nashorn engine, JAVA 8 introduces a new command line tool, jjs, to execute javascript codes at console.
Interpreting js File
Create and save the file hello.js in C:\hitechpoints\workspace\java8\src> A folder.
hello.js
function printName(name) {
print('Hello, ' + name);
}
printName('Elavarasan !!');
Open console and use the following command.
C:\hitechpoints\workspace\java8\src>jjs hello.js
Output:
Hello, Elavarasan !!
jjs in Interactive Mode
Open the console and use the following command.
C:\hitechpoints\workspace\java8\src>jjs
jjs> function printName(name) {
...> print('Hello, ' + name);
...> }
jjs> printName('Elavarasan !!');
Hello, Elavarasan !!
Pass Arguments
Open the console and use the following command.
C:\hitechpoints\workspace\java8\src>jjs -- Elavarasan!!
jjs> function printName(name) {
...> print('Hello, ' + name);
...> }
jjs> printName(arguments[0])
Hello, Elavarasan!!
Calling JavaScript from Java
Using ScriptEngineManager, JavaScript code can be called and interpreted in Java.
Example: Executing JavaScript file in Java Code
You can execute JavaScript file directly from your Java file. In the following code, we are reading a file hello.js with the help of FileReader class. Create the following Java program using any editor of your choice in, say C:\hitechpoints\workspace\java8\src>.
Java8Nashorn.java
import java.io.FileReader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Java8Nashorn {
public static void main(String[] args) throws Exception{
// 1. Creating script engine
ScriptEngine ee = new ScriptEngineManager().getEngineByName("Nashorn");
// 2. Reading Nashorn file
ee.eval(new FileReader("C:\\hitechpoints\\workspace\\java8\\src\\hello.js"));
}
}
Output:
Compile the class using javac compiler as follows −
C:\hitechpoints\workspace\java8\src>javac Java8Nashorn.java
Now run the Java8Nashorn as follows −
C:\hitechpoints\workspace\java8\src>java Java8Nashorn
It should produce the following result −
Hello, Elavarasan !!
Example: Embedding JavaScript Code in Java Source File
You can embed your JavaScript code in Java source file. Java compiler will not complaint but it is not good practice when you have large source code. In the following example, we are evaluating JavaScript code.
Java8Nashorn.java
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Java8Nashorn {
public static void main(String[] args) throws Exception{
// 1. Creating script engine
ScriptEngine ee = new ScriptEngineManager().getEngineByName("Nashorn");
// 2. Reading Nashorn code
ee.eval("print('Hello, Elavarasan !!')");
}
}
Compile the class using javac compiler as follows −
C:\hitechpoints\workspace\java8\src>javac Java8Nashorn.java
Now run the Java8Nashorn as follows −
C:\hitechpoints\workspace\java8\src>java Java8Nashorn
It should produce the following result −
Hello, Elavarasan !!