我正在构建一个简单的记事本应用程序。我想用 Electron.js 构建前端,对于文件处理或数据库,我想使用 Java 作为后端语言。我应该如何连接Electron和Java后端。请大家帮忙。
最佳答案
Electron 结合了 Chromium 渲染引擎和 Nodejs 运行时。 Electron 本身就是后端。 Electron 有 2 个主要部分。渲染器和主进程。主流程将构建在 Nodejs 之上。 因此,要连接 Java 代码,您应该将 Java 代码构建为可执行 bin 文件。我们可以这样在 Electron 之间进行通信
const { spawn } = require('child_process');
const javaExternalService = spawn(cliPath, [args]);
javaExternalService.stdout.on('data', data => {
console.log(`Daemon stdout: ${data}`);
resolve(data.toString());
// Here is where the output goes
});
javaExternalService.stderr.on('data', data => {
console.log(`Daemon stderr: ${data}`);
resolve(data.toString());
// Here is where the error output goes
});
javaExternalService.on('close', code => {
console.log(`Daemon closing code: ${code}`);
// Here you can get the exit code of the script
});
但这会降低你的 Electron 性能。如何在 Electron 主进程中连接和管理数据库?
关于java - 如何将 Electron.js 与桌面应用程序的 Java 代码连接起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59418311/