Running Java from Node.js

Christian Dittrich

creatale GmbH

WebDev BBQ, 15.09.2015

Use Case

node-dv

ZXing in Node.js

What now?

DIY?

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

2. Java to C/C++

j2c

XMLVM

  • http://xmlvm.org/overview/
  • cross platform mobile development
  • problems with some java.nio classes ZXing uses (Paths, Files, DirectoryStream)

Tangible

Java2C

JCGO

GCJ

  • http://gcc.gnu.org/java/
  • GNU
  • supports only Java 1.4 + some 1.5 features
  • last updated in 2009
  • java.nio missing (ZXing uses nio)

Toba

XES

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

4. Java to JavaScript

XMLVM

Java2Script

GWT

  • http://www.gwtproject.org
  • by Google
  • for Web Applications
  • needs explicit support by project (has to be packaged into a GWT module)

GWT Exporter

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

  1. Execute Java
  2. Java to C/C++
  3. JNI
  4. 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);
});

sharpen

Tangible

XES

Octopus

Java Language Conversion Assistant 2.0

VaryCode

Finally: The Solution

XMLVM

http://xmlvm.org/overview/

  1. build JAR
  2. compile to .NET-DLL via XMLVM
  3. 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

Example

Coming soon to GitHub

'zxing-edge' node module