Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I execute a node.js script from a Java Class?

Tags:

java

node.js

I am currently developing a Java system that communicates with node.js using socket.io. The system and the script are on the same server. How can I execute the script from my Java code and keep it alive from my app?

like image 711
B. TIger Avatar asked Dec 15 '22 10:12

B. TIger


1 Answers

Note when using process builder the path to your JavaScript file is an argument and "node" is the command, so they need to be separated:

ProcessBuilder pb = new ProcessBuilder("node", "app.js");

This is also useful for inheriting its console output, starting the process and getting the reference to the process:

pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
Process p = pb.start();
like image 196
willbush Avatar answered Jan 03 '23 20:01

willbush