How to call Java program from NodeJs

You can simply call a java command , with classpath & arguments, using module node-java-caller


You can start a child process, and send a kill signal when you don't need it.

var spawn = require('child_process').spawn;
var child = spawn('java', ['params1', 'param2']);

To kill the application, or to simulate a CTRL+C, send a signal:

// control + c is an interrupt signal
child.kill('SIGINT');

// or send from the main process
process.kill(child.pid, 'SIGINT');

If you're going to run the application detached, you should probably write the PID somewhere. To run the application detached, run it like this:

var fs = require('fs');
var out = fs.openSync('./out.log', 'a');
var err = fs.openSync('./out.log', 'a');

var child = spawn('java', [], {
  detached: true,
  stdio: [ 'ignore', out, err ]
});
child.unref();

This spawns a child process whose I/O streams aren't associated with the parent process.

Tags:

Java

Node.Js