An Introduction to Running JavaScript in Java with Nashorn



This ‘teaser’ article is intended to whet the appetite of the reader to learn more. Nashorn is a JavaScript engine developed by Oracle. It aims at allowing JavaScript to be embedded in Java, and to develop standalone JavaScript applications.

JavaScript is one of the fastest growing and most widely adopted languages today. Having started out as a language to provide interactivity in a browser, it has grown multi-fold. In 2009, Node.js brought it to server side programming. And in 2013, it came to Windows desktops when Microsoft released the Metro (or Modern) apps, which allowed developers to write desktop apps using JavaScript.

Fast forward to 2016 and JavaScript is everywhere! With new language features, a huge list of libraries and an ever growing community, it’s getting bigger by the day.

Introducing Nashorn
The JavaScript language needs to be executed on a JavaScript engine. Some very popular engines out there are V8 (Chrome and Node), Chakra (Microsoft Edge) and Spider Monkey (Mozilla Firefox). Similarly, Nashorn is the JavaScript engine in Java, which enables embedding JavaScript in Java applications.

Up and running with Nashorn
Cutting right to the chase, there are two ways in which you can execute JavaScript in Java:
1. Using the JJS command line tool
2. Directly inside Java code, using the native JavaScript engine

Using the JJS tool
JJS is a REPL (Read Evaluate Print Loop) tool inbuilt into JDK. Running JavaScript code with it is as simple as navigating to the JAVA_HOME\bin directory and executing the jjs.exe or file.
Here is an example.

jjs> var x = ‘Hello OSFY’;
jjs> print(x);
Hello OSFY

Using the Java API
The general purpose of executing JavaScript in a Java environment is to create a ScriptEngine object, and pass your JavaScript to the script engine to evaluate. So the Hello World code will be:

package com.metalop.nashorn.tutorials;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class HelloWorld {
               public static void main(String[] args) throws ScriptException {
                              ScriptEngine engine = new ScriptEngineManager().getEngineByName(“nashorn”);
                              engine.eval(“print(‘Hello World!’);”);

Some of the shortcomings of Nashorn

  • Nashorn is just a JavaScript engine, and doesn’t support any of the DOM (Document Object Model) level APIs supported by browsers. So you can’t use things like console.log statements or AJAX requests and any DOM dependent libraries like jQuery inside Nashorn.
  • Nashorn currently supports only ECMA Script 5.1, and therefore you can’t write JS with the ES6 and ES2017 syntax. (Later in the article, we will look at how to fix this using Babel.js.)

Some advanced examples
Using moment.js inside Nashorn: moment.js is a pretty lightweight and wonderful isomorphic JavaScript library that makes things like DateTime formatting, date localisation and DateTime arithmetic work like a breeze. Any Java developer would agree that formatting dates in Java with multiple try-catch blocks is pretty messy, and would appreciate the ease moment.js brings to the task.

To get started, download moment.js from  and dump it into your Resources folder. The code eval1 for evaluating in Java Runtime can be downloaded from

Writing react.js/es6 syntax and transpiling it with Babel in Nashorn: As mentioned earlier, Nashorn currently supports only ES 5.1 syntax, and you need to run it through a transpiler like Babel before executing it on the Nashorn engine.

The good thing about Babel is that it comes in a JavaScript standalone version (which can be downloaded from It’s the complete Babel library in a single JS file, which can be evaluated on Nashorn and then be re-used to transpile ES6 JS into ES 5.1 JavaScript in the code.

Download the library, dump it into the Resources folder of the project, and use the trans1 from the downloaded from the above link to see the translation at runtime.

This article just covers the tip of the iceberg as you can do a lot more with Nashorn like creating UIs with JavaFX, rendering HTML mark-up on the server side, etc. You can find examples for more scenarios (along with the above mentioned examples) at


Please enter your comment!
Please enter your name here