Running Java from Node.js – Christian Dittrich – creatale GmbH



Running Java from Node.js – Christian Dittrich – creatale GmbH

0 0


webdevbbq-java-nodejs


On Github creatale / webdevbbq-java-nodejs

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

errors when compiling ZXing no Java class library implementation provided

XMLVM

problems with some java.nio classes ZXing uses (Paths, Files, DirectoryStream)

Tangible

Java2C

only limited subset of Java supported (no generics, ZXing needs generics) last updated in 2011

JCGO

GCJ

supports only Java 1.4 + some 1.5 features last updated in 2009 java.nio missing (ZXing uses nio)

Toba

XES

3. JNI

has problems with memory management (frequent out of memory exceptions), waiting for GC kills performance

4. Java to JavaScript

XMLVM

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);
});

sharpen

Tangible

XES

Octopus

Java Language Conversion Assistant 2.0

VaryCode

Finally: The Solution

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

Example

Coming soon to GitHub

'zxing-edge' node module

Running Java from Node.jsChristian Dittrichcreatale GmbHWebDev BBQ, 15.09.2015