Java Nashorn JavaScript Engine: Integrate JavaScript with High Performance
Discover the Nashorn JavaScript Engine, introduced in Java 8, a high-performance engine that compiles JavaScript into bytecode for enhanced execution speed. Learn how Nashorn enables you to embed JavaScript within Java, execute JavaScript from command-line tools, and seamlessly call Java methods within JavaScript code using the jjs command.
Java - Nashorn JavaScript Engine
Introduction to Nashorn
Nashorn is a powerful and high-performance JavaScript engine introduced in Java 8. It replaces the older Rhino engine and offers performance improvements of up to 10 times faster. Nashorn compiles JavaScript code directly into bytecode, leveraging dynamic features introduced in Java 7 to enhance performance.
With Nashorn, you can execute JavaScript code from command-line tools, embed JavaScript in Java files, and call JavaScript methods from Java code. Additionally, using the jjs
command, you can call Java methods within JavaScript code.
Execute JavaScript via Command-Line Tools
Java 8 introduces the jjs
command-line tool to execute JavaScript code. The tool can interpret both JavaScript files and code snippets. Additionally, you can invoke Java methods from JavaScript using this tool.
Example: Running a JavaScript File
First, create and save a file sample.js
in the C:\JAVA
folder with the following content:
print('Hello World!');
To execute the JavaScript file, open the console and run:
C:\JAVA>jjs sample.js
Output:
Now, update the file to include a function and call it:
function sayMessage(){ print('Hello World!'); } sayMessage();
Run the updated file:
C:\JAVA>jjs sample.js
Output:
Execute JavaScript Directly in Command Prompt
You can also use jjs
interactively by typing it in the command prompt:
C:\JAVA>jjs jjs> print("Hello, World!") jjs> quit()
Output:
Passing Arguments to jjs
You can pass command-line arguments to jjs
, which are stored in the arguments
array.
Example:
Run the following command in the console:
C:\JAVA>jjs -- a b c
Output:
Calling JavaScript from Java
Using the ScriptEngineManager
class (introduced in Java 6), you can load the Nashorn engine and execute JavaScript code from within Java. Java variables can also be passed into JavaScript.
Example:
import javax.script.ScriptEngineManager; import javax.script.ScriptEngine; import javax.script.ScriptException; public class Java8Tester { public static void main(String args[]) { ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn"); String name = "Mahesh"; Integer result = null; try { nashorn.eval("print('" + name + "')"); result = (Integer) nashorn.eval("10 + 2"); } catch(ScriptException e) { System.out.println("Error executing script: " + e.getMessage()); } System.out.println(result.toString()); } }
Output:
12
Calling Java from JavaScript
With the jjs
tool, you can also call Java code from within JavaScript. In this example, we'll use the BigDecimal
class in JavaScript to perform calculations.
Example:
Create and save sample.js
:
var BigDecimal = Java.type('java.math.BigDecimal'); function calculate(amount, percentage) { var result = new BigDecimal(amount).multiply(new BigDecimal(percentage)) .divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_EVEN); return result.toPlainString(); } var result = calculate(568000000000000000023,13.9); print(result);