2

I have a node.js script and a java program in the same folder (.class and .java and .js). I want to call the java program from the node.js script. In terminal I can call the java program like this

java -cp java-json.jar:. PlutoMake "tests/android.png"

I saw this thread How to call Java program from NodeJs

and I am trying to do the same thing, here is the node.js code

var child = spawn('java', ['-cp java-json.jar:. PlutoMake', 'tests/android.png']);

This seems to run without crashing, but then nothing happens. The java program creates an image, but if I do it through node, it doesn't work. Does anyone know whats wrong?

Thanks

Community
  • 1
  • 1
sneaky
  • 2,141
  • 9
  • 31
  • 38

2 Answers2

6

The array of arguments you pass should have one element per argument. You’re incorrectly combining a few of them.

var child = spawn('java', ['-cp', 'java-json.jar:.', 'PlutoMake', 'tests/android.png']);

Checking the output and exit code could prove useful:

child.on('close', function (exitCode) {
    if (exitCode !== 0) {
        console.error('Something went wrong!');
    }
});

// If you’re really just passing it through, though, pass {stdio: 'inherit'}
// to child_process.spawn instead.
child.stderr.on('data', function (data) {
    process.stderr.write(data);
});
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • That worked, thanks, but now how can I set an event that runs when the java script exits or finishes? – sneaky Aug 16 '15 at 19:39
  • @sneaky: I edited the answer to write about checking the exit code for errors, but that’s also the event you’d use to continue (after confirming that the exit code *was* 0). – Ry- Aug 16 '15 at 19:40
  • In java I know I can do like `system.exit(0)` and I think that triggers the first function above. Which one triggers when the java finishes executing (without calling exit and without throwing exception)? And also how does the second function above get triggered? – sneaky Aug 16 '15 at 19:41
  • @sneaky: 0 is success. It is the default exit code. You don’t have to call `system.exit` for this to work. The second event is fired when there’s error output from the child process. – Ry- Aug 16 '15 at 19:42
  • In my usecase I used Node.js as a tool which just needed to call some Java utility and wait for answer. In such case `execFileSync` is more suitable than `spawn` (also the method returned stdout as Node.js Buffer which had to be converted into string). – Tomáš Záluský Feb 13 '18 at 08:16
  • Any idea why the output is always piped through the error output stream and the standard output stream (even when the exit code is 0)? – Pranav Oct 10 '18 at 09:10
  • @Pranav: Where do you want it to go instead, and why would the exit code affect where the output went? – Ry- Oct 10 '18 at 15:36
  • Sorry if I wasn't clear. I meant, why (assuming that the java code executes correctly with an exit code of 0) does the output show up in the stderr stream rather than the stdout stream? Wouldn't the stderr stream only be written to if the JVM encountered an error and so wrote the exception and stack trace to the error stream? – Pranav Oct 12 '18 at 02:23
  • @Pranav: The Java program can write to the stream it prefers. Lots of types of messages intended as diagnostic or for the user might end up on stderr, especially if there’s structured output on stdout. You can check the difference with bash: `java YourProgram > /dev/null`. If you still see output, it’s on stderr. – Ry- Oct 12 '18 at 03:19
0

You can simply call a java command , with classpath & arguments, using module node-java-caller, it embeds the call to spawn and will also automatically install java if not present on the system

Nicolas Vuillamy
  • 564
  • 7
  • 14