Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we're going to explore how Java integrates with JavaScript using different engines. Can anyone tell me which JavaScript engine was originally used in Java?
Was it Rhino?
That's correct! Rhino was the default engine up until Java 7. Now, starting from Java 8, a new engine called Nashorn was introduced. Why do you think that was?
Maybe it was to improve performance?
Exactly! Nashorn provided better performance and integration with Java. Remember this acronym: 'NICE' which stands for Nashorn Improves Compiled Execution. How about we discuss Rhino for a moment? What do you know about it?
I think it was developed by Mozilla.
That's true! Rhino was indeed developed by Mozilla. Now, let's summarize Nashorn's benefits before we move on.
Let's see how Nashorn works with Java. I'll show you a simple example of running a script that prints a message.
Is it similar to how we run Java programs?
Yes, it is! In fact, here's a snippet: 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!');'); } }. What do you think happens when we run this?
It prints 'Hello from JavaScript!'?
Exactly right! This example shows how you can easily integrate a JavaScript snippet in Java code. Let's think about what we can use this capability for.
Now that we understand how Nashorn works, let's discuss its deprecation. Why do you think Nashorn was deprecated in Java 11?
Perhaps there were better alternatives?
Yes, that's correct! Developers are now encouraged to use newer alternatives like GraalVM. These solutions allow for high-performance execution of various languages. Why is it important to have alternatives when one technology is deprecated?
So the development process doesn't get stuck and we can keep improving?
Spot on! Innovation is crucial in technology. As we move forward, I recommend checking out GraalVM for harmonizing multiple languages within your Java applications.
Tools for running JavaScript within Java applications.
The earlier JavaScript engine used in Java applications.
An improved engine introduced in Java 8, now deprecated in newer versions.
A standard interface for integrating scripting languages into Java.
If you're running scripts on Java's stage, use Nashorn; it’s the latest rage.
Once upon a time in a realm of code, Rhino ruled the lands. But one day, Nashorn arrived and brought fast speeds that made coding lighter and faster. Together, they transformed the Java environment.
Remember the acronym 'RNN' - Rhino to Nashorn - New and nimble!
NICE: Nashorn Improves Compiled Execution.
Term
Rhino
Click to flip
Term
Nashorn
Click to flip
Term
Java Scripting API
Click to flip
An open-source JavaScript engine developed by Mozilla used in Java applications until Java 7.
A more efficient JavaScript engine introduced in Java 8, designed to replace Rhino with improved performance.
An API that enables scripting language support in Java applications.
An interface that represents an engine used for executing scripts.
A class responsible for managing different script engines.
A map of key-value pairs used to pass variables between Java and scripts.
An interface containing the execution context for a script, including input/output and variable scope.
This section discusses the JavaScript engines in Java, focusing on the transition from Rhino to Nashorn and how these engines enable Java applications to run JavaScript code.
The JavaScript engine in Java has evolved from using Rhino to Nashorn, allowing seamless integration of JavaScript into Java applications. While Nashorn improved performance and integration, it has been deprecated since Java 11, paving the way for newer solutions like GraalVM.
In this section, we delve into the evolution of JavaScript engines used within Java, specifically focusing on Rhino and Nashorn. Rhino, developed by Mozilla, served as the default JavaScript engine until Java 7. With the introduction of Nashorn in Java 8, improvements in performance and integration were made. Nashorn allows Java applications to execute JavaScript code more efficiently. However, as of Java 11, Nashorn has been deprecated and removed in Java 15. Despite its deprecation, Nashorn remains a significant educational tool, as it exemplified how Java can embed and execute JavaScript through the Java Scripting API. This section highlights basic examples of using the Nashorn engine to execute scripts and pass data between Java and JavaScript, demonstrating its capabilities and significance in the context of Java's evolving dynamic scripting capabilities.