20

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

5 Answers 5

14

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

Sign up to request clarification or add additional context in comments.

2 Comments

nodyn is no longer being actively maintained. (from nodyn github site).
...Like avatar-js
12

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

3 Comments

I tried calling jshint using Runtime.getRuntime().exec(...), but I keep getting the exception: java.io.IOException: Cannot run program "jshint" CreateProcess error=2, The system cannot find the file specified; From regular command line it works just fine, though... I have no idea whet the problem is :(
you just mentioned connecting with a running node.js app, but this is not the case. There are solutions to run node.js apps inside jvm as mentioned by @Somatik.
This answer is completely unrelated, I wonder why it has up votes!!
2

Yes, It is very eassy to execute and node.js file using java.

import java.io.FileReader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class RunScriptFileDemo {
  public static void main(String[] args) {
      ScriptEngineManager manager = new ScriptEngineManager();
      ScriptEngine engine = manager.getEngineByName("js");
      try {
         FileReader reader = new FileReader("yourFile.js");
         engine.eval(reader);
         reader.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

1 Comment

This won't run Node.js programs. It will only run plain javascript programs. As other commenters have said, one would need to add Avatar.js, nodyn, or trireme to get any features of Node.js to work.
1

Check out https://github.com/caoccao/Javet, you can embed a node.js runtime inside a java application and share variables between both.

Comments

-1

Cant be done. For normal JS you can use Rhino, but for Node you will need to make sure it is in the PATH then call Runtine.exec or a ProcessBuilder with ByteArrayOutputsreams that can later be cinverted to strings. The node code cannot access Java and vice versa.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.