NASHORN
JavaScripting in Java8
Presentation by,
Hina Firdaus
M.tech(CSE)
2
nd
Semester
Guided by,
Dr. Syed Imtiyaz Hassan
Assistant Professor
SEST
Index
What is Nashorn?
Why is Nashorn?
Features of Nashorn.
Shortcomings of Nashorn.
Jjs.
Summary.
What is Nashorn?
Nashorn is replacement introduced with Java 8 for the old, slow
Rhino JavaScript engine.
It is based on the Da Vinci Machine (JSR 292/javax.script) and has
been released with Java 8
The project aim will be to allow embedding JavaScript in Java
applications via JSR-223 and to develop standalone JavaScript
applications.
Nashorn comprises of an embedded JavaScript interpreter and a
command line tool.
Nashorn, pronounced
"nass-horn," is German
for "rhinoceros," and it's
one of the animal names
for a German tank
destroyer used in World
War II.
Why Nashorn?
Nashorn scripts do not need to be compiled. Nashorn fully supports
ECMAScript 5.1 specification along with some extensions, to 100% pure Java
implementation and 100% complied to bytecode and not the interpreted
mode as with Rhino.
Nashorn provides much better performance than Rhino engine.
It directly compiles the JavaScript code in-memory and passes the Java
bytecode to the JVM, and uses invokedynamic which provides backward
compatibility to JDK7.
Nashorn provides a flexibility to the users that they can either
programmatically use it from Java programs or they can utilize it from the
command line tool “jjs” which is located in “$JAVA_HOME/bin” directory.
It provides much faster execution and multi-threading of JavaScript while
running in Java’s JRE.
Nashron can help in executing the OS level commands very easily.
So far no security risk or exposure has been identified with Nashorn.
Features of Nashorn.
Interoperable:
Nashorn provides interoperability between the Java and
JavaScript i.e., Java code can call JavaScript code, and vice
versa.
Embeddable:
It’s very easy to use Nashorn to extend your Java applications
with JavaScript code as there are no third party libraries required
to add on.
Compatible
The goal pursued by the Nashorn team is not to provide a fast
JavaScript implementation for a specific browser - it’s to provide
a very fast implementation for server-side JavaScript
applications and scripts embedded in Java applications.
Continue…
Performant
Nashorn uses the invokedynamic JVM instruction to implement
all of its invocations. That is a major component of the
comparative improvement of performance and memory usage
regarding Mozilla Rhino.
Shell Scripting
Nashorn provides several built-in functions to exit the script, print
and read lines from standard output and input, read files, load
scripts, and bind properties of objects.
JavaFX
User can write JavaFX applications entirely in JavaScript using
Nashorn.
Shortcomings of Nashorn.
Nashorn will only support ECMAScript-262 Edition 5.1. It will not
support any features of Edition 6 or any nonstandard features
provided by other JavaScript implementations.
Nashorn will not include a browser plug-in API.
Nashorn will not include support for DOM/CSS or any related
libraries (such as jQuery, Prototype, or Dojo).
Nashorn will not include direct debugging support.
jjs
For Nashorn engine, JAVA 8 introduces a new command line tool, jjs to
execute java script code at console.
Steps to create and execute jjs script:
Step 1: Interpreting js file.
Step 2: jjs in interactive mode
Step 3: pass arguments
Step 4: Calling JavaScript in JAVA
Using ScriptEngineManager, JavaScript code can be called and interpretted in java.
Step 5: Verify the result.
Step 6: Calling Java from JavaScript
Summary
Project Nashorn has enhanced the original JDK 6 Rhino
implementation by greatly improving performance for longer running
applications, for example when used inside a web server.
It provides a timely update of performance and capabilities of the
co-bundled JavaScript engine.