Introducing the Nashorn JavaScript Engine

By Aldo Ziflaj

Nashorn is a new JavaScript engine developed in the Java programming language by Oracle, released with Java 8. Nashorn’s goal is to implement a lightweight high-performance JavaScript runtime in Java with a native JVM. By making use of Nashorn, the developer can embed JavaScript in a Java application and also invoke Java methods and classes from the JavaScript code.

Why Leave Rhino?

Rhino is the predecessor of Nashorn. It began as a project in 1997 at NetScape and got released in 1998.

There have been 16 years since the release of Rhino, and that JavaScript engine has lived its days. So the Java guys decided to have some fun by developing a new JavaScript engine from scratch instead of rewriting the existing one. This gave birth to Nashorn (fun fact: nashorn means rhino in German).

Almost everyone here has been using JavaScript in the browser, and some have been using it on the server (like Node.js), but Nashorn is developed for another purpose. By using Nashorn the developer can perform the magic of:

  • Running JavaScript as native Desktop code.
  • Using JavaScript for shell scripting.
  • Call Java classes and methods from JavaScript code.

The Goals of Nashorn

When Nashorn was designed, the developers decided a set of goals for it:

  • It should be based on ECMAScript-262 Edition 5.1 language specification, and must pass the ECMAScript-262 compliance tests.
  • It should support the javax.script (JSR 223) API.
  • It should allow invoking Java from JavaScript and vice-versa.
  • It should define a command-line tool, jjs for evaluating JavaScript code in “shebang” scripts (that normally start with #!/bin/sh), here documents, and edit strings.
  • Its performance should be significantly better than Rhino.
  • It should have no security risks.

Furthermore, no one decided that Nashorn will not include debugging and not support CSS and JavaScript libraries/frameworks. This means Nashorn can be implemented in a browser without being a nightmare.

JavaScript in a (nut)Shell

In order to use JavaScript in the shell by using Nashorn’s jjs tool, you should first install the JDK8, which you can download for free. To test its installation, execute:

>_ javac -version
# it should echo
# java version "1.8.x"
jjs -version
# it should echo
# nashorn 1.8.x

If you face any problems with the first or the second command, try adding JDK in the path

Now you can use JavaScript as a shell script. Check out this simple example:

jjs> var a = 1
jjs> var b = 4
jjs> print (a+b)

As you may have already figured out, you don’t have to write the code into the jjs shell. You can write the code into a JavaScript source file, and then call it from the shell. Consider the following JavaScript code:

var isPrime = function(num) {
    if (isNaN(num) || !isFinite(num) || num < 2) 
        return false;

    var m = Math.sqrt(num);

    for (var i = 2;i <= m; i++) 
        if (num % i === 0) 
            return false;

    return true;

var numbers = [ 2, 3, 4, 5, 6, 7, 8, 9, 10 ];

for (var i = 0; i < numbers.length; i++) {
    if (isPrime(numbers[i]))
        print(numbers[i] + " is prime");
        print(numbers[i] + " is not prime");

Assuming the code is on a file called prime.js, we can run it in the shell, by executing:

>_ jjs prime.js
2 is prime
3 is prime
4 is not prime
5 is prime
6 is not prime
7 is prime
8 is not prime
9 is not prime
10 is not prime

This may remind you of Python code or bash scripting, but it is JavaScript. And to make it more bash-like, Nashorn gives the arguments variable to extract the command line arguments. Consider this example:

if (arguments.length === 0)
    print("No command-line arguments.");
else {
    print("Called with these command-line arguments:");

    for each (cli_arg in arguments) {

Running it will give this output (arguments go after --):

>_ jjs cliargs.js
No command-line arguments.

>_ jjs cliargs.js -- a b "c d e"
Called with these command-line arguments:
c d e

Also, JavaScript can use Java classes and methods. See this example of a multithreaded JavaScript code:

var Thread = Java.type("java.lang.Thread"); 
var Runnable = Java.type('java.lang.Runnable');

var Run1 = Java.extend(Runnable, { 
    run: function() { 
        print("One thread");
        print("One thread");

new Thread(function() {
    print("Another thread");
    print("Another thread");
    print("Another thread");

new Thread(new Run1()).start();

And the output would be:

Another thread
Another thread
One thread
One thread
Another thread

You can tell by the output that the code is multithreaded. By using Java.type("java.lang.Thread"); we can call Java classes inside the JavaScript code. Nashorn allows even going in the other direction, calling JavaScript code inside Java code.

package j2js.example;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Main {

    public static void main(String[] args) {

        ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("nashorn");
        try {
            nashorn.eval("print('Am I Java or JavaScript?')");
        } catch (ScriptException e) {


This example just prints the Am I Java or JavaScript? question on line 14, but this is the simplest example of JavaScript code into Java. One can read the whole source code from the JavaScript file, using Java methods, and then pass that code as a String parameter to the eval() method. This would make the JavaScript code to execute inside Java.


Nowadays JavaScript is everywhere! You may use it for client-side applications, server-side applications, and better yet, sometimes for both client and server. You may use it for mobile applications or to set up a small IoT. And now, with Nashorn, you may use it as a powerful shell-like scripting language, by taking advantage of the simplicity of JavaScript and the rich API of Java.

  • Piyush Chauhan

    Great Post.

  • mikemurko

    Nashorn is a “rhino” in terms of its speed. It starts up extremely slow and the first round of javascript execution is as well … after a while you will see it speed up. This makes it basically useless if simply building and running javascript once in Java. I have tested as well, but read more here:

  • Denis

    Rhino is not engine that ships with Firefox. Rhino is written with java, but firefox uses SpiderMonkey (or IonMonkey in last releases).

  • acjohnson55

    Is Nashorn backward compatible with Java 7 at all?

    • Kon Soulianidis

      Not intentionally but in the early days of Java 8 dev there was someone outside of oracle with a fork to work on Java 7,, however no longer maintained.

      Best advice is to upgrade to Java 8 though, Nashorn is still getting worked on with each minor update release, so you’ll get all the performance improvements as they come as well as upcoming support for upcoming ES6 syntax features.

      If you are really stuck with Java 7 then using the older Javascript engine Rhino is ok. The Java code for calling and running JS from within Java is identical and so once you move to Java 8 you’ll start using Nashorn anywhere you’ve asked for the ‘javascript’ ScriptEngineManager

    • Kon Soulianidis

      No acjohnson55.

      During development of Java 8 someone forked the Nashorn project and made it Java 7 compatible. However the port is no longer maintained.

      That said, Nashorn is still being enhanced and improved. So its worth upgrading to Java 8 anyway to take advantage of the speed improvements still coming, as well as support for ES 6 features that Rhino wont have.

      Java 7 and prior have the Rhino Javascript engine as the article mentions.

  • mikemurko

    Thx for deleting comment. Are you guys not software developers or something? Just marketers?

  • chrisseaton

    “Rhino is the JavaScript engine that ships with Mozilla Firefox” is that true? I thought they used SpiderMonkey in Firefox?

    • Daniel

      “Rhino is the JavaScript engine that ships with Mozilla Firefox”
      That is not, and has never been, the case.

    • Luke Bonaccorsi

      Nope, they’re wrong. Rhino is another engine maintained by Mozilla, but SpiderMonkey is the one in their browser.

    • Aldo Ziflaj

      Yeah, you are right. I was misinformed about it. Thanks for noticing it. It is now removed

  • Anonymous

    > Now, Rhino is the JavaScript engine that ships with Mozilla Firefox.

    I’m pretty sure that Rhino is not the engine that’s shipping with Firefox; it’s a product of Mozilla, yes, but not shipping with Firefox. The current JavaScript engine in Firefox is called SpiderMonkey and in C++ and not Java.

  • Joakim Bengtson

    Actually NodeJS was built with shell scripting in mind as well and does support the shebang syntax.

  • Scott

    Not sure why I’d use jjs over node for shell scripting, but I’m excited to try using JavaScript to build desktop apps. I’d love to see some benchmarks comparing Nashorn and Node.js

  • Greg Meyer

    My team has been trying to use nashorn for some hardcore server side stuff, instead of rhino.

    Oracle is not, explicitly, supporting debugging and has no interest in supporting HTML or XML (and, I assume, CSS goes along with that).

  • mooduino

    “Furthermore, no one decided that Nashorn will not include debugging and not support CSS and JavaScript libraries/frameworks.”

    That’s not very clear.



Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in JavaScript, once a week, for free.