Can I call Java from Node.js via JNI and how?

Is it possible to call Java from Node.js via JNI ? Are there any examples?

+42
java jni
Jan 18 2018-11-18T00:
source share
7 answers

It looks complicated. Node.JS runs on the Google Chrome V8 JavaScript engine . You will need to create a V8 C ++ binding ( v8 C ++ Crash Course shows an example) that starts the JVM and does all the JNI processing.

I think you might be better off disabling JavaServer and Node.js over the network (someone wrote an example using RabbitMQ for messaging based on Java / Node.js). Here JSON will be a great data exchange format (if you trust that your Java server produces the correct JSON, you can just have it () in Node).

+7
Jan 18 '11 at 9:17
source share

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)); // prints "item2" 

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.

+99
Feb 19 '12 at 20:14
source share

Communication can be done using child_process.

create a new process and execute the main class. This main class returns console output. therefore, data is available for the node stdout data event.

 var cmd = require('child_process').spawn('java', ['Main']); cmd.stdout.on('data', function (data) { console.log('stdout: ' + data); // This will print string returned by Main class. }); 
+5
Jan 6 '12 at 10:18
source share

I donโ€™t know all the details of Node.js, but I assume that your mention of JNI is actually a Java Native interface. You can use JNI only in Java, so imho does not make sense to access Java from JNI if you are not already in java.

It seems like this is the wrong approach, and you need to find Node.js doco for your integration chapter ...

0
Jan 18 2018-11-21T00:
source share

I wonder if it is possible at all. but even if itโ€™s possible, I think itโ€™s difficult to implement, and Iโ€™m sure that no one has done it yet.

what about using named pipe for communication between processes (java and node.js)?

0
Jan 18 2018-11-18T00:
source share

I think you're looking for a native extension to use as a bridge. Although I don't have an example of what you are saying, I have an example of how to invoke a C ++ extension using Node.JS

https://github.com/jrgleason/NodeJSArduinoLEDController

0
May 10 '12 at 3:13
source share

I think the approved answer is a bit outdated. Maybe worth a try: https://github.com/joeferner/node-java

0
Oct 23 '15 at 4:31 on
source share



All Articles