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 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.
- 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 - 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/ - Rename the native library to
avatar-js.dylib
and rename the jar toavatar-js.jar
and put both in a directory calleddist
- Create a simple Node.js app in the a file called
app.js
: - Run the command:
java -Djava.library.path=dist -jar dist/avatar-js.jar app.js
The output should be: - Navigate to http://localhost:8000/
Server running at http://127.0.0.1:8000/