8

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?

1
  • 1
    Shouldn't it be spawn('java',[-jar', 'done.jar', 'etc...'])? Commented Mar 24, 2015 at 21:00

1 Answer 1

18

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.

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

2 Comments

Thanks Brad, that did it! For anyone who has the same problem I didn't need to specify the full path to java.
Worked for me too! However , i had to mention the full path of the jar file.

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.