Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js run a java program?

Tags:

node.js

I have a simple nodejs server up and running. I am running into trouble when I try to spawn a child process. the jar file I am trying to access, is in the same directory as the node script. It takes in a command line argument that I am trying to pass in and it outputs data to the command line, which I am trying to pipe into the var child.

var child = require('child_process').spawn('java -jar done.jar',['argument to pass in']);

child.stdout.on('data', function(data) {
    console.log(data.toString());
});

child.stderr.on("data", function (data) {
    console.log(data.toString());
});

This produces the following error message:

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: spawn java -jar done.jar ENOENT
    at exports._errnoException (util.js:746:11)
    at Process.ChildProcess._handle.onexit (child_process.js:1046:32)
    at child_process.js:1137:20
    at process._tickCallback (node.js:355:11)

Any ideas?

like image 647
mathJohn Avatar asked Mar 24 '15 20:03

mathJohn


1 Answers

You're executing java. -jar and done.jar are arguments to pass.

var child = require('child_process').spawn(
  'java', ['-jar', 'done.jar', 'argument to pass in']
);

You will also need to specify the full path to java, or make sure that java is specified in the OS path.

like image 130
Brad Avatar answered Nov 14 '22 23:11

Brad