Nashorn
By Oracle
Nashorn is a JavaScript engine that Oracle built into the Java Development Kit to run JavaScript code on the Java Virtual Machine, letting Java applications execute scripts and letting JavaScript code call into Java classes. It shipped as…
Definition
Nashorn is a JavaScript engine that Oracle built into the Java Development Kit to run JavaScript code on the Java Virtual Machine, letting Java applications execute scripts and letting JavaScript code call into Java classes. It shipped as the default jjs command-line tool and scripting engine starting with JDK 8, replacing the older Rhino engine, before being deprecated and eventually removed from the JDK.
Overview
Nashorn was introduced by Oracle as part of JDK 8 to address the limitations of Rhino, the JavaScript engine Java had bundled for years through the javax.script API. Rhino was interpreter-based and had fallen behind the performance and specification compliance expected of a modern JavaScript runtime, so Oracle rewrote the engine from scratch with a focus on generating actual JVM bytecode from JavaScript source rather than interpreting it line by line. Mechanically, Nashorn compiles JavaScript into Java bytecode at runtime using the invokedynamic instruction introduced in JDK 7, which was specifically designed to support dynamically typed languages running on the JVM. This let Nashorn achieve substantially better performance than Rhino for many workloads, since the JVM's just-in-time compiler could optimize the generated bytecode much as it would for native Java code. Nashorn also exposed a two-way bridge: JavaScript code could instantiate and call Java classes directly, and Java code could invoke JavaScript functions through the standard ScriptEngine interface. Among JVM-hosted JavaScript engines, Nashorn sat between the older, simpler Rhino and the later GraalJS, which Oracle developed as part of the GraalVM project. GraalJS eventually superseded Nashorn by offering closer compliance with modern ECMAScript versions and better performance through GraalVM's polyglot compilation infrastructure, and Oracle used this as the rationale for retiring Nashorn. In practice, Nashorn was used for embedding scripting capability inside Java applications, such as letting end users customize application behavior with small JavaScript snippets, powering build tool plugins, or gluing together Java libraries from lightweight scripts without a full Java compile cycle. It was invoked either programmatically via ScriptEngineManager or from the command line via the jjs tool that shipped alongside java and javac. Oracle deprecated Nashorn in JDK 11 and removed it entirely in JDK 15, recommending GraalJS as the replacement for teams that still needed JVM-based JavaScript execution. This makes Nashorn a poor choice for any new project: it no longer ships with current JDKs, receives no security updates, and lacks support for JavaScript language features added after its final release. Existing code that depends on it must migrate to GraalJS or move the scripting logic out of the JVM entirely. The removal also affected build tools such as older versions of Maven and Gradle plugins that had used Nashorn for configuration scripting, forcing those ecosystems to adapt as well, and it left some legacy enterprise applications needing a migration plan long after the engine's initial deprecation notice.
Key Features
- Compiled JavaScript to real JVM bytecode using the invokedynamic instruction
- Replaced Rhino as the default scripting engine bundled with the JDK
- Exposed a two-way bridge between JavaScript code and Java classes
- Shipped with the jjs command-line tool for running scripts directly
- Integrated with the standard javax.script ScriptEngine API
- Deprecated in JDK 11 and removed from the JDK in JDK 15
- Superseded by GraalJS as Oracle's recommended JVM JavaScript engine