Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

java - How to run a jar file with node.js child_process API?

I tried to run a jar file on nodejs but it threw out a following error:

Error: Unable to access jarfile /home/example/Applications/example.jar

This is the following code that I have in my test.js:

var exec = require('child_process').exec, child;
child = exec('/usr/bin/java -jar ~/Applications/example.jar',
  function (error, stdout, stderr){
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if(error !== null){
      console.log('exec error: ' + error);
    }
});

I ran my test.js with nodejs in this command but received the error above:

node test.js

Is there any mistake that I made with my code? I am not sure why it is throwing an error at this point.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

remove .jar from exec();

java will find the jar file without .jar when using the -jar argument. else its like.. searching for filename.jar.jar

special snowflake macos requires the .jar and does not work if you omit it.
(thanks to G???) he also created a related question


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...