JavaScript made easier with Java 8

Lochana Ranaweera
Lochness Writes
Published in
3 min readDec 10, 2015

Want to have JavaScript code in your Java 8 application? I might be able to help you with that! :)

You probably are aware of JavaScript in terms of scripting web browsers. The JavaScript engines have access to the HTML document object model (DOM) in web browsers, therefore able to manipulate HTML elements through the DOM. Also you must have heard of scripting in the form of Node.js in servers. Node.js is built on Google Chrome’s V8 JavaScript engine to provide for a JavaScript runtime in servers.

The solution to be able to evaluate JavaScript code on the Java Virtual Machine during runtime is to use ‘Nashorn’.

So what exactly is Nashorn?

Developed by Oracle and released with Java 8, the Nashorn JavaScript Engine enables Java developers to embed JavaScript code in Java applications and also invoke Java methods and classes from the JavaScript code.

Before Nashorn was released with Java 8, Java bundled ‘Rhino’ as the JavaScript engine as the implementation of the JavaScript language written to run on the Java Virtual Machine. However, with time it was established that Rhino cannot provide for an efficient JavaScript runtime, hence the Nashorn Project was born to come up with a lightweight high-performance JavaScript runtime.

Apart from the above utilities, Nashorn also defines a command-line tool called jjs which is used to invoke the Nashorn engine. Thereafter, it can be used to evaluate JavaScript code from the command line itself.

Today, I’ll be giving a short introduction on how to use jjs tool and later I will post on invoking Nashorn from Java code.

So, how do we run Nashorn from the command line?

To run Nashorn from the command line simply type jjs. Of course you have to first install JDK 8.0

Here, you have a basic REPL, “read-eval-print” loop. Whenever you enter an expression, its value is printed after being evaluated by the JavaScript engine. Also you can experiment with Java APIs as below and have access to common java classes, packages etc

Tips:

You might already be annoyed when trying to use Nashorn from the command line as it is not that user friendly! When you press the Tab key you normally get a list of possible command completions. However, even though this sort of thing is possible with an environment like Scala REPL, JavaScript REPL cannot support this as it is a dynamically typed language.

You might also be annoyed by the fact that you can’t recall previous commands using and arrow keys in the jjs command line. I used this tool called rlwrap to enable command recall.

If you are using Ubuntu, to install rlwrap:

Open up a new terminal and type,

sudo gedit /etc/apt/sources.list

Here we are going to check if our universe repository is enabled by inspecting the above file. You will need sudo to ensure that you have permission to edit the file sources.list. Then if the universe is not included modify the file by including the following line.

deb http://us.archive.ubuntu.com/ubuntu vivid main universe

When you’re done save the file and close it. Now update your system by running the command

sudo apt-get update

Once update finishes, install the rlwrap package by running

sudo apt-get install rlwrap

Now we are done. To run jjs with rlwrap support simply run it as rlwrap jjs instead of jjs.

--

--