Friday, March 21, 2014

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

Jjs
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 java.net 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 java.net Maven Repo.
    Current version is here: https://maven.java.net/content/repositories/public/com/oracle/avatar-js/0.10.25-SNAPSHOT/
    Example file: avatar-js-0.10.25-20140313.063039-43.jar 

  2. Download the native library avatar-js.dylib from the java.net Maven Repo.
    Current version is here: https://maven.java.net/content/repositories/public/com/oracle/libavatar-js-macosx-x64/0.10.25-SNAPSHOT/
    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 http://127.0.0.1:8000/

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

4 comments:

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

    http://strongloop.com/strongblog/how-to-run-node-js-on-the-jvm-with-avatar-js-and-loopback/

    ReplyDelete
  2. This is awesome! Thanks for sharing. I will start playing a bit with this and with the Project Avatar too (https://avatar.java.net/index.html)

    ReplyDelete
  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"

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

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...