How to run a jar file with node.js child_process API?

If you are using it on windows command prompt you can use this code.

var exec = require('child_process').exec, child;
child = exec('java -jar C:\\..\\..\\yourjar.jar',
function (error, stdout, stderr){
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if(error !== null){
  console.log('exec error: ' + error);
}
});

Dont forget the double slaces or else it will be a mess.


remove .jar from exec();

java will find the jar file without .jar when using the -jar argument. else its like.. searching for filename.jar.jar

special snowflake macos requires the .jar and does not work if you omit it.
(thanks to Gʀɪᴍ) he also created a related question