Java Platform, Standard Edition Nashorn User's Guide
Contents    Previous    Next

1 Introduction

This chapter provides introductory information about the Nashorn engine and how it can be used to interpret JavaScript code in a Java application or from the command line.

The Nashorn engine is an implementation of the ECMAScript Edition 5.1 Language SpecificationFoot1. It was fully developed in the Java language as part of the Nashorn projectFoot2. The code is based on the new features of the Da Vinci MachineFoot3, which is the reference implementation of Java Specification Request (JSR) 292: Supporting Dynamically Typed Languages on the Java PlatformFoot4.

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 jjs or jrunscript tool.


Nashorn is the only JavaScript engine included in the JDK. However, you can use any script engine compliant with JSR 223: Scripting for the Java PlatformFoot1, or implement your own. For more information, see Java Scripting Programmer's Guide at


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:

  1. Import the javax.script package.

    The Java Scripting API is composed of classes and interfaces in this package. For more information about the javax.script package, see the specification at

  2. Create a ScriptEngineManager object.

    The ScriptEngineManager class is the starting point for the Java Scripting API. A ScriptEngineManager object is used to instantiate ScriptEngine objects and maintain global variable values shared by them.

  3. Get a ScriptEngine object from the manager using the getEngineByName() method.

    This method takes one String argument 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: "Nashorn", "javascript", "JavaScript", "js", "JS", "ecmascript", "ECMAScript".

After you have the Nashorn engine instance, you can use it to evaluate statements and script files, set variables, and so on. Example 1-1 provides simple Java application code that evaluates a print("Hello, World!"); statement using Nashorn.


The eval() method throws a ScriptException that must be handled properly.

For more information about using scripts in Java code, see Java Scripting Programmer's Guide at

Footnote Legend

Contents    Previous    Next

Copyright © 1993, 2021, Oracle and/or its affiliates. All rights reserved.