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
j2c
errors when compiling ZXing
no Java class library implementation provided
XMLVM
problems with some java.nio classes ZXing uses (Paths, Files, DirectoryStream)
Java2C
only limited subset of Java supported (no generics, ZXing needs generics)
last updated in 2011
GCJ
supports only Java 1.4 + some 1.5 features
last updated in 2009
java.nio missing (ZXing uses nio)
3. JNI
has problems with memory management (frequent out of memory exceptions), waiting for GC kills performance
Java2Script
compiled code contains errors
when compiling ZXing, some java.nio stuff missing
GWT
needs explicit support by project (has to be packaged into a GWT module)
GWT Exporter
needs annotation of source code with @Export() and implements Exportable
Kotlin
- http://kotlinlang.org
- a JVM language by JetBrains
- compiles to JavaScript
- Java can be automatically translated to Kotlin
errors when compiling ZXing to JavaScript
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
Running Java from Node.jsChristian Dittrichcreatale GmbHWebDev BBQ, 15.09.2015