You should try the node-java npm module, which is a well-written shell over JNI.
node-jave does not seem to have widespread use yet, but when playing with it, I was impressed by how simple and robust it is.
It's simple:
var list = java.newInstanceSync("java.util.ArrayList"); list.addSync("item1"); list.addSync("item2"); console.log(list.getSync(1));
You can do anything with your built-in JVM - create objects, call methods, access fields, etc.
There is a slight mismatch in the impedance between Node and Java, so if you intend to interact with something complex, I would recommend writing most of your interactions in Java and exposing a simpler interface to the Node / Java barrier. It just makes debugging easier.
--- Dave
ps, RealWorldUseCase (tm): I worked in a place where there was a rather complicated (and spaghetti-coded) protocol between several browser clients and a Java-based service. I wrote a pretty nice test harness that used jsdom to host N simulated browsers and used node-java as a wrapper around the Java service code. It was trivial to pin transport interfaces in both JS for clients and Java for service, so whenever any of these things sends a message, I grab it and put it in the queue for the probabilistic delivery of the intended target (i.e. I virtualized the network). Thus, I can run a full-scale simulation of several clients interacting with the Java service, and through it, and run it all in one process without any wired connection. And then I could do funny things like deliberately reordering message delivery to make sure the code is robust against synchronization errors. And when the error was discovered, I had ordered messages and they could play them in order to reproduce the error. Oh, and it was all set up and managed by a rather complicated script with several thousand lines of logging and ended in less than 1 second per turn. 2 weeks well spent. Funny stuff.
RealWorld Use Case # 2: selenium-inproc is a module that wraps the SeleniumRC JAR file, providing a Node interface for testing w / Selenium browser automation without having to run another localhost service.
Dave Dopson Feb 19 '12 at 20:14 2012-02-19 20:14
source share