0

I want to run a java command with Nodejs

This command works with me on bat file

"C:\Program Files\Java\jdk1.8.0_121\bin\java.exe" -Xmx1024m -XX:+UseParallelGC -Dfile.encoding=UTF-8  -classpath "<path>\app\netlogo-6.1.1.jar" org.nlogo.headless.Main  --model modelname.nlogo --setup-file setup.xml --experiment experiment1 --table table-output.csv

How can i run this command with Nodejs ? Have I to use Node child_process, and it's possible to run this after deploying my app on server ?

1
  • You can use Child Process to run and spawn commands Commented Oct 15, 2019 at 21:15

1 Answer 1

1

If you want it executed in the main application asynchronously:

const { exec } = require('child_process');
exec('YOUR COMMAND HERE', (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
  console.error(`stderr: ${stderr}`);
});

or with a child process that will report back to your primary application via event emitter:

const { spawn } = require('child_process');
const YOUR_COMMAND = spawn('YOUR_COMMAND', ['--arg1', 'value1', '--arg2', 'value2']);

YOUR_COMMAND.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

YOUR_COMMAND.stderr.on('data', (data) => {
  console.error(`stderr: ${data}`);
});

YOUR_COMMAND.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

But this is where you should go: https://nodejs.org/api/child_process.html, my answers are just retreads of the provided examples

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

Comments

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.