Running Java from Node.js
WebDev BBQ, 15.09.2015
Requirements
- easy to keep up to date (automatically?)
- platform "independent" (at least Windows and Linux)
- easy to use API (has to work with buffers, not just files)
- performant
1. Execute Java
- childProcess.spawn
- node-jvm
- client + server (aka service)
- not capable of working directly with buffers (only
public static void main(String[] args)
entry points) - usually extra processes
XMLVM
- http://xmlvm.org/overview/
- cross platform mobile development
- problems with some java.nio classes ZXing uses (Paths, Files, DirectoryStream)
3. JNI
- 'java' node module
- ok-ish API
var list = java.newInstanceSync("java.util.ArrayList");
- has problems with memory management (frequent out of memory exceptions), waiting for GC kills performance
Recap
- Execute Java
- Java to C/C++
- JNI
- Java to JavaScript
5. Java to C# + 'edge' node module
What is edge?
var edge = require('edge');
var helloWorld = edge.func(function () {/*
async (input) => {
return ".NET Welcomes " + input.ToString();
}
*/});
helloWorld('JavaScript', function (error, result) {
if (error) throw error;
console.log(result);
});
Java Language Conversion Assistant 2.0
XMLVM
http://xmlvm.org/overview/
- build JAR
- compile to .NET-DLL via XMLVM
- use 'edge' node module and IKVM to run in-process on Node.js
- easy to keep up to date
- platform independent (Mono)
- usable API (same classes and methods for every object in C# + litte JavaScript boilerplate)
- about 10% performance loss for ZXing, compared to original Java code
Coming soon to GitHub
'zxing-edge' node module