Friday, March 21, 2014

Running Node.js applications on the JVM with Nashorn and Java 8

I am naively playing around with Nashorn. Nashorn is the new JavaScript Runtime that comes with Java 8 and runs on the JVM.

In this post I show how to run simple Node.js applications with Nashorn on the JVM.

Avatar.js is a project to bring the node programming model, APIs and module ecosystem to the Java platform. Unfortunately Avatar.js does not officially provide a binary distribution and the build process looks quite intricate (involving Python and C++).

There is also Project Avatar, an upcoming web-platform by Oracle. Project Avatar builds upon Avatar.js.

By reverse-engineering Project Avatar I found the necessary artefacts of Avatar.js in the Maven repository. These artefacts can be used to run Node applications with Avatar.js without having to build Avatar.js yourself.

The following steps show how to run a simple Node.js web application on OSX. The steps should be very similar for Linux or Windows.
  1. Download avatar-js.jar from the Maven Repo.
    Current version is here:
    Example file: avatar-js-0.10.25-20140313.063039-43.jar 

  2. Download the native library avatar-js.dylib from the Maven Repo.
    Current version is here:
    Example file: libavatar-js-macosx-x64-0.10.25-20140312.062209-35.dylib
    For Linux you would download the corresponding .so file from ../libavatar-js-linux-x64/0.10.25-SNAPSHOT/
    For Windows you would download the corresponding .dll file from ../libavatar-js-win-x64/0.10.25-SNAPSHOT/

  3. Rename the native library to avatar-js.dylib and rename the jar to avatar-js.jar and put both in a directory called dist

  4. Create a simple Node.js app in the a file called app.js:

  5. Run the command:
    java -Djava.library.path=dist -jar dist/avatar-js.jar app.js
  6. The output should be:
    Server running at

  7. Navigate to http://localhost:8000/


  1. I posted a blog a few months ago and built a script to automate the build.

  2. This is awesome! Thanks for sharing. I will start playing a bit with this and with the Project Avatar too (

  3. Just tried this, one change to your instructions - where you write "Rename the native library to avatar-js.dylib " I had to actually rename to "libavatar-js.dylib" or I'd get "Exception in thread "main" java.lang.UnsatisfiedLinkError: no avatar-js in java.library.path"

  4. For anyone else getting UnsatisfiedLinkError on Linux, I had to rename the "libavatar-js.dylib" to ""


Related Posts Plugin for WordPress, Blogger...