29.3.2 - Basic Nashorn Example
Enroll to start learning
You’ve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take practice test.
Introduction & Overview
Read summaries of the section's main ideas at different levels of detail.
Quick Overview
Standard
In this section, a simple Java application utilizing the Nashorn JavaScript engine is presented, showcasing how to execute JavaScript code. The example illustrates the ease of embedding script execution in Java, which is critical for leveraging dynamic behavior in applications.
Detailed
Basic Nashorn Example
The Basic Nashorn Example illustrates the integration of Java and JavaScript using the Nashorn engine, a JavaScript engine developed for Java SE 8. This engine allows Java applications to execute JavaScript code directly, thereby enhancing flexibility and expanding functionality. The example provided includes a Java program that demonstrates how to create a ScriptEngine, retrieve the Nashorn JavaScript engine, and execute a simple JavaScript statement that prints text to the console.
Here’s a breakdown of the key components:
1. Importing the Script Package: The javax.script.* package is imported to access the necessary scripting classes.
2. Creating a ScriptEngineManager: This manager is responsible for creating and managing instances of ScriptEngine.
3. Retrieving the Nashorn Engine: Using `getEngineByName(
Youtube Videos
Audio Book
Dive deep into the subject with an immersive audiobook experience.
Basic Structure of a Nashorn Script
Chapter 1 of 1
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
import javax.script.*;
public class ScriptExample {
public static void main(String[] args) throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
engine.eval("print('Hello from JavaScript!');");
}
}
Detailed Explanation
This chunk introduces a very basic example of using the Nashorn JavaScript engine in Java. The program begins by importing the necessary classes from the javax.script package. It then defines a public class named ScriptExample with a main method that serves as the entry point. Within this method, an instance of ScriptEngineManager is created, which is responsible for managing script engines. Next, a ScriptEngine is obtained for the Nashorn JavaScript engine using the getEngineByName method. Finally, the eval method of the engine is called, which allows JavaScript code (in this case, a simple print statement) to be executed directly, displaying 'Hello from JavaScript!' on the console. This example demonstrates how to integrate and run JavaScript code within a Java application.
Examples & Analogies
Think of it like a chef (Java) who can suddenly speak another language (JavaScript) to take instructions directly from a guest (the script). By understanding and executing these instructions, the chef can create dishes (dynamic applications) without needing to know how to cook every recipe beforehand. The Nashorn engine allows this seamless communication.