Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a node.js script from inside java

Tags:

How can I call a node.js inside java and save the console.log values in a String variable?

like image 990
Spaniard89 Avatar asked Jan 11 '13 09:01

Spaniard89


People also ask

Can I use node js code in JavaScript?

Introduction. Node. js is a popular open-source runtime environment that can execute JavaScript outside of the browser using the V8 JavaScript engine, which is the same engine used to power the Google Chrome web browser's JavaScript execution.

How do I run a node js script?

The usual way to run a Node. js program is to run the globally available node command (once you install Node. js) and pass the name of the file you want to execute. While running the command, make sure you are in the same directory which contains the app.


2 Answers

Check these projects which allow you to run node.js scripts inside the jvm

  • https://github.com/apigee/trireme (Apigee)
  • http://nodyn.io/ (Redhat)
  • https://avatar-js.java.net/ (Oracle)
like image 185
Somatik Avatar answered Sep 22 '22 15:09

Somatik


It is possible for a Java application to communicate with a running Node.JS application. For instance, you can have a Node.JS app running on an available port and the Java app can communicate with it via tcp sockets.

http://nodejs.org/api/net.html

Or you can create an http server and expose a rest service which your Java app can consume.

http://nodejs.org/api/http.html

Or as md_5 says, you can use Runtime.exec and then call getInputStream on the resulting process.

http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html

The ways you can communicate between node.js and Java are no different from other cross application communication that can be done.

It is also possible to invoke Java code from your Node.JS application using something like node-java.

https://github.com/nearinfinity/node-java

like image 45
Timothy Strimple Avatar answered Sep 18 '22 15:09

Timothy Strimple