Java8 - Introduction to the Nashorn JavaScript engine

Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 07:59:00 Viewed : 235


Nashorn is a JavaScript engine introduced in Java 8 as part of the Java Development Kit (JDK). Its a modern JavaScript engine that is part of the Java platform and allows seamless integration of JavaScript code with Java applications. Nashorn replaces the older Rhino JavaScript engine and brings improved performance and better compatibility with ECMAScript 5.1.

Key Features of Nashorn:

  1. Performance: Nashorn is designed to provide better performance compared to the Rhino engine. It uses modern JavaScript optimization techniques and Just-In-Time (JIT) compilation to achieve improved execution speed.

  2. Integration with Java: Nashorn allows seamless integration of JavaScript code with Java applications. You can easily call Java code from JavaScript and vice versa.

  3. ECMAScript 5.1 Support: Nashorn supports ECMAScript 5.1, which is a standardized version of JavaScript. This compatibility makes it easier to work with modern JavaScript code and libraries.

  4. Command-Line Interface: Nashorn includes a command-line interface that allows you to run JavaScript code directly from the command line.

Example Usage:

Here is a simple example demonstrating how Nashorn can be used to evaluate JavaScript code within a Java application:

java
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class NashornExample { public static void main(String[] args) { // Creating a script engine manager ScriptEngineManager engineManager = new ScriptEngineManager(); // Getting the Nashorn JavaScript engine ScriptEngine engine = engineManager.getEngineByName("nashorn"); try { // Evaluating a simple JavaScript expression Object result = engine.eval("print(`Hello, Nashorn!`); 10 + 5;"); System.out.println("Result: " + result); } catch (ScriptException e) { e.printStackTrace(); } } }

In this example, the ScriptEngineManager is used to obtain the Nashorn JavaScript engine (nashorn). The eval method is then used to execute a simple JavaScript code that prints a message and performs a mathematical operation. The result is then printed from the Java application.

Running JavaScript from Command Line:

You can also use Nashorn from the command line. Save the following JavaScript code in a file (e.g., example.js):

javascript
// example.js var message = `Hello from Nashorn!`; print(message);

Then, run it using the Nashorn engine:

bash
jjs example.js

This will execute the JavaScript code using Nashorn and print the message.

Nashorn provides a powerful and efficient way to work with JavaScript in Java applications, enabling interoperability between the two languages. However, its worth noting that as of JDK 11, Nashorn has been deprecated, and developers are encouraged to use other JavaScript engines or frameworks for new projects.

Search
Related Articles

Leave a Comment: