nodejs fs.openSync()带有'a'Java程序运行失败

nodejs fs.openSync() with the flags 'a' fails to run java program

本文关键字:Java 程序 运行 失败 fs 带有 nodejs openSync      更新时间:2023-09-26

我有一个问题,我做了一个Twitter Bot,涉及一个处理java程序,在飞行中生成一个图像,在EC2服务器上运行:成功工作了几天后,我开始从终端得到以下错误消息,因为没有图像得到由程序生成了。

想知道如何解决这个问题,让我的机器人回到正轨。任何建议吗?

fs.js:432
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^
Error: ENOENT, no such file or directory 'postcards/output.png'
    at Object.fs.openSync (fs.js:432:18)
    at Object.fs.readFileSync (fs.js:286:15)
    at processing (/home/ubuntu/bot.js:141:29)
    at ChildProcess.exithandler (child_process.js:641:7)
    at ChildProcess.EventEmitter.emit (events.js:98:17)
    at maybeClose (child_process.js:743:16)
    at Socket.<anonymous> (child_process.js:956:11)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Pipe.close (net.js:466:12)
ubuntu@ip-172-31-46-35:~$ 

这是我调用的生成图像和发布tweet的主要函数:

var exec = require('child_process').exec; 
var fs = require('fs'); 
function tweetIt(txt) {
    //Making this program launch processing sketch and execute it // I just need to use node to invoke this file in the terminal $ node bot.js
    //var cmd = 'processing-java --sketch=`pwd`/postcards --run';
    var cmd = 'postcards/postcards'; 
    exec(cmd, processing); // command to execute the sketch and callback function to call when done
    function processing() {
        var filename = "postcards/output.png";
        var params = {
            encoding: 'base64'
        }
        var b64content = fs.readFileSync(filename, params);
        T.post('media/upload', {
            media_data: b64content
        }, uploaded); // just uplading my image to twitter before being able to tweet it
        console.log("done with the image!");
        function uploaded(err, data, response) {
            // this is where the tweet including the image happens
            var id = data.media_id_string;
            var tweet = {
                status: txt,
                media_ids: [id]
            }
            T.post('statuses/update', tweet, tweeted); // type, params, callbackfunction
        }
        function tweeted(err, data, response) {
            if (err) {
                console.log(err);
            } else {
                console.log("It works!");
            }
        }
    }
}

路径有问题?尝试使用__dirname(当前文件的路径)

的相对路径
fs.readFile(__dirname+"/../myfolder/picture.png",...)