Running Node.js applications on the JVM with Nashorn and Java 8
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.
avatar-js.jarfrom 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.dylibfrom 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
.sofile from ../libavatar-js-linux-x64/0.10.25-SNAPSHOT/
For Windows you would download the corresponding
.dllfile from ../libavatar-js-win-x64/0.10.25-SNAPSHOT/
- Rename the native library to
avatar-js.dyliband rename the jar to
avatar-js.jarand put both in a directory called
- Create a simple Node.js app in the a file called
- 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/