简体   繁体   中英

Node.js run a java program?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM