Note:The Nashorn engine, the
jjstool, and the modules jdk.scripting.nashorn and jdk.scripting.nashorn.shell are deprecated in JDK 11 in preparation for removal in a future release.
The Nashorn engine is an implementation of the ECMAScript Edition 5.1 Language Specification. It also implements many new features introduced in ECMAScript 6 including template strings;
const, and block scope; iterators and
WeakSet data types; symbols; and binary and octal literals.
It was fully developed in the Java language as part of the Nashorn Project. The code is based on the new features of the Da Vinci Machine, which is the reference implementation of Java Specification Request (JSR) 292: Supporting Dynamically Typed Languages on the Java Platform.
The Nashorn engine is included in the Java SE Development Kit (JDK). You can invoke
Nashorn from a Java application using the Java Scripting API to interpret embedded scripts, or
you can pass the script to the
Invoking Nashorn from Java Code
To invoke Nashorn in your Java application, create an instance of the Nashorn engine using the Java Scripting API.
To get an instance of the Nashorn engine:
Import the javax.script package.
The Java Scripting API is composed of classes and interfaces in this package.
ScriptEngineManagerclass is the starting point for the Java Scripting API. A
ScriptEngineManagerobject is used to instantiate
ScriptEngineobjects and maintain global variable values shared by them.
ScriptEngineobject from the manager using the
This method takes one
Stringargument with the name of the script engine. To get an instance of the Nashorn engine, pass in
"nashorn". Alternatively, you can use any of the following:
After you have the Nashorn engine instance, you can use it to evaluate statements and script files, set variables, and so on. The following example,
EvalScript.java, provides simple Java application code that evaluates a
print("Hello, World!"); statement using Nashorn.
eval() method throws a
ScriptException that must be handled properly.
Java Scripting API Examples with Java Classes in Java Platform, Standard Edition Java Scripting Programmer's Guide demonstrates how to use scripts in Java code.
Invoking Nashorn from the Command Line
There are two command-line tools that can be used to invoke the Nashorn engine:
jrunscript: This is a generic command that invokes any available script engine compliant with JSR 223. By default, without any options,
jrunscriptinvokes the Nashorn engine, because it is the default script engine in the JDK.
jjs: This is the recommended tool, created specifically for Nashorn. To evaluate a script file using Nashorn, pass the name of the script file to the
jjstool. To launch an interactive shell that interprets statements passed in using standard input, start the
jjstool without specifying any script files.
Nashorn Parser API
The Nashorn parser API enables applications, in particular IDEs and server-side frameworks, to parse and analyze ECMAScript code.
Parse ECMAScript code from a string, URL, or file with methods from the Parser class. These methods return an instance of CompilationUnitTree, which represents ECMAScript code as an abstract syntax tree. The package jdk.nashorn.api.tree contains the Nashorn parser API.