can I call Java from Node.js via JNI? Are there any examples?
-
1`Node.js ` seems java script file and JNI is used to access native functionality, can you please elaborate your question – jmj Jan 18 '11 at 21:08
-
1If you can use Rhino instead of Node.js, the interfacing is really easy: https://developer.mozilla.org/en-US/docs/Scripting_Java – Janus Troelsen Apr 05 '13 at 23:54
5 Answers
You should try the node-java npm module which is a well-written wrapper over JNI.
node-jave doesn't appear to (yet) have broad adoption, but playing with it, I've been impressed with how straightforward and robust it has been.
It's as simple as:
var list = java.newInstanceSync("java.util.ArrayList");
list.addSync("item1");
list.addSync("item2");
console.log(list.getSync(1)); // prints "item2"
You can do just about anything with your embedded JVM - create objects, call methods, access fields, etc.
There is a slight impedance mismatch between Node and Java, so if you are going to interact with something complicated, I'd recommend writing most of your interactions in Java and exposing a simpler interface across the Node/Java barrier. It just makes for easier debugging that way.
--- Dave
p.s., RealWorldUseCase(tm): I worked at a place that had a pretty complex (and spaghetti-coded) protocol between multiple browser clients and a Java-based service. I wrote a pretty sweet test-harness which used jsdom to host N simulated browsers and used node-java as a wrapper around the Java service code. It was trivial to shim out the transport interfaces, both in JS for the clients, and in Java for the service, so whenever any of these things sends a message, I capture that and stick it in a queue for probabilistic delivery to the intended target (ie, I virtualized the network). In this way, I could run a full-on simulation of multiple clients interacting with and through a Java service, and run the whole thing inside a single process without any wire communication. And then I could do fun stuff like deliberately reorder message deliveries to make sure the code was resilient to timing bugs. And when a bug was discovered, I had the message orderings logged and could reproduce them to repro the bug. Oh, and the whole thing set up and ran a pretty complex scenario with a few thousand lines of logging and finished in under 1 second per run. 2-weeks well spent. Fun stuff.
RealWorld Use Case #2: selenium-inproc - a module that wraps the SeleniumRC JAR file providing a node interface to browser automation testing w/ Selenium without having to run yet another localhost service.

- 41,600
- 19
- 95
- 85
-
Does this work with third party libraries such as Gurobi? http://www.gurobi.com/ – Karthik Balakrishnan Oct 18 '15 at 06:58
That looks tricky. Node.JS runs on the Google Chrome JavaScript engine V8. What you will have to do is to create a V8 C++ binding (v8 c++ Crash Course shows an example) that starts a JVM and does all the JNI handling.
I think you might be better off letting a JavaServer and Node.js communicate via the network (someone wrote an example for using RabbitMQ for Java/Node.js message based communication). Here, JSON would be a great data exchange format (if you trust your Java server produces proper JSON you can just eval() it in Node).
-
1Interesting to know that. but I guess that network access, would have to be at least an order of magnitude slower that JNI. Don;t you think? – Kay Pale Jan 18 '11 at 21:54
-
5It depends on what you want to do. The JNI approach will be a LOT of work and a hell to debug (JNI alone is a pain already and it will be a lot worse with V8 -> C++ -> JNI -> Java). – Daff Jan 18 '11 at 22:08
-
-
2I was thinking more of an actual messaging queue e.g. like ZeroMQ (http://zero.mq/) – Daff Jun 16 '11 at 12:56
-
2Dave's answer [here](http://stackoverflow.com/a/9352818/3196753) (>60 upvotes) should be the accepted answer, which speaks about the node-java npm module. – tresf Apr 01 '15 at 17:54
I think what you are looking for is a native extension to use as a bridge. Although I don't have an example of what you are saying, I do have an example on how to call a C++ extension using Node.JS

- 21,969
- 32
- 147
- 289
I am not aware of all the details of Node.js but i am assuming that your mentioning of JNI is actually the Java Native Interface. One can only use JNI from Java, so imho it does not make sense to access Java from JNI if you are not already in java.
It would appear that this is the wrong approach, and you need to search the Node.js doco for their integration chapter...

- 18,002
- 10
- 71
- 105
-
7Actually, JNI also covers JVM invocation, not just native calling from Java. – Lawrence Dol Jan 18 '11 at 22:12
-
@software monk, your statement is quite right, but in ctx of the q, its hard to imagine node js using jni at all w/ all its javaisms. – mP. Jun 09 '12 at 12:50
I wonder if it is possible at all. but even if it is possible I imagine it is hard to implement and I am certain that nobody has done that yet.
how about using a named pipe to communicate between processes(java and node.js) ?

- 60,935
- 33
- 147
- 186