0

So i currently have a Nodejs application that spawns a child process that executes a java application and this is working just fine when ran directly from the command prompt.

http.createServer(function (request, response) {

console.log('Started Executing Request! \n' );

const { exec } = require('child_process');
exec('"C:\\Program Files\\Java\\jdk1.8.0_172\\bin\\java.exe" -jar "C:\\Temp\\myjava.jar"', (err, stdout, stderr) => {
  if (err) {
      console.log('There was an error! ' + err);
    // node couldn't execute the command
    return;
  }

  // the *entire* stdout and stderr (buffered)
  console.log('stdout: ' + stdout);
  console.log('stderr: ' + stderr);
});

console.log('Finished Executing Request! \n' );
}).listen(8087);

// Console will print the message
console.log('Server running at http://127.0.0.1:8087/ \n');

The problem i have is when put this into a service it doesn't seem to want to execute the java application. I have it outputing to a log file and i do have the "Started Executing Request" and the "Finished Executing Request!" within the log but the java is not executed.

AdamH409
  • 83
  • 1
  • 8

2 Answers2

1

Please be sure to set your user account on your service's Log On tab:

enter image description here

Specify the account you are logged in to when you "run directly from the command prompt", so that Java can find important environment variables it needs to do its work (e.g. JAVA_HOME).

Those environment variables are probably not available in your "Local System" account, which is why you have trouble running java there...

CoreTech
  • 2,345
  • 2
  • 17
  • 24
  • Hi CoreTech, thanks for the response. Actually a regular HelloWorld.jar file runs just fine from within the service ... the problem is the jar file that i'm trying to run is trying to print to a local printer and this seems to be not able to run. As i've stated this all works when i run it directly from the command prompt which is what has me baffled. I have also changed the Logon user to Administrator and that didn't seem to make a difference. It seems that if my jar is doing anything more then just printing text it doesn't seem to want to work from a service. – AdamH409 Jan 11 '19 at 21:47
  • Have you tried setting the **same user account** that you are logged in to when it runs properly from the command prompt? Administrator may not have all the necessary software installed. – CoreTech Jan 12 '19 at 15:37
0

I was trying to print to a label printer so what this turned out to be a problem with the printer driver itself. Seems printing from a service is prone to issues so i went a different route. What i ended doing was i created a self hosted desktop application that minimized to the system tray on windows. The above application worked properly within mac and linux.

AdamH409
  • 83
  • 1
  • 8