论坛上提供了不同的答案,但我提供了运行Java程序所涉及步骤的分解。
步骤1.从yourCode.java创建.jar文件。(需要在系统上安装Java开发工具包)。
链接https://www.tecmint.com/create-and-execute-jar-file-in-linux/
中说明了该过程
完成后,将.jar文件放置在node.js应用程序的父目录中,并使用以下代码。这是一个完整的功能,可以从Express应用程序调用。
const executeJava = () => {
return new Promise((resolve,reject) => {
const child = exec('java -jar main.jar',function (error,stdout,stderr) {
console.log('Value at stdout is: ' + stdout); // here you get your result. In my case I did'nt needed to pass arguments to java program.
resolve(stdout);
if (error !== null) {
console.log('exec error: ' + error);
reject(error);
}
});
})
}
// you can call this function from your node.js express app using
// const myResult = await executeJava();
本文链接:https://www.f2er.com/3076127.html