Java Reference
In-Depth Information
The interpreter runs on the JVM and executes programs written in the source
• The source language ships with a compiler that produces class files from units
of source language code.
The resulting compiled class files are then directly executed on the JVM, usu‐
ally with some additional language-specific runtime support.
Nashorn takes the second approach—but with the added refinement that the com‐
piler is inside the runtime, so that JavaScript source code is never compiled before
program execution begins. This means that JavaScript that was not specifically writ‐
ten for Nashorn can still be easily deployed on the platform.
Nashorn is unlike many other JVM languages (such as JRuby)
in that it does not implement any form of interpreter. Nashorn
always compiles JavaScript to JVM bytecode and executes the
bytecode directly.
This is interesting, from a technical perspective, but many developers are curious as
to what role Nashorn is intended to play in the mature and well-established Java
ecosystem. Let's look at that role next.
Nashorn serves several purposes within the Java and JVM ecosystem. Firstly, it pro‐
vides a viable environment for JavaScript developers to discover the power of the
JVM. Second, it enables companies to continue to leverage their existing investment
in Java technologies while additionally adopting JavaScript as a development lan‐
guage. Last, it provides a great engineering showcase for the advanced virtual
machine technology present in the HotSpot Java Virtual Machine.
With the continued growth and adoption of JavaScript, broadening out from its tra‐
ditional home in the browser to more general-purpose computing and the server
side, Nashorn represents a great bridge between the existing rock-solid Java ecosys‐
tem and a promising wave of new technologies.
For now, let's move on to discuss the mechanics of how Nashorn works, and how to
get started with the platform. There are several different ways in which JavaScript
code can be executed on Nashorn, and in the next section we'll look at two of the
most commonly used.
Executing JavaScript with Nashorn
In this section, we'll be introducing the Nashorn environment, and discuss two dif‐
ferent ways of executing JavaScript (both of which are present in the bin subdirec‐
tory of $JAVA_HOME ):
Search WWH ::

Custom Search