electron 做桌面软件,想嵌入java 应用,需要用到jre1.8,redis(win3.xx,mac 7.x),mysql8 并且能分别打包win和mac版本。

软件手动启动命令如下:

#jre

./bin/java

# redis

./redis-server redis.conf

# mysql (默认密码:Usefullc20220606%)

./bin/mysqld  --defaults-file=my.conf

在窗口创建启动前,引用代码如下所示:

//启动系统依赖进程

// redis

let cmdHomePath = getExecuteResourcesPath('redis/');

let cmdPath = cmdHomePath + 'redis-server';

let cmdParam = cmdHomePath + 'redis.conf';

exec_system_file(cmdPath,[cmdParam],function(message){

// websocketWs.send(message)

// 将文件内容发送回渲染进程

// event.sender.send('export-response', excelData);

});

//mysql

cmdHomePath = getExecuteResourcesPath('mysql/');

cmdPath = cmdHomePath + 'bin/mysqld';

cmdParam = '--defaults-file='+cmdHomePath+'my.conf';

exec_system_file(cmdPath,[cmdParam],function(message){

console.log(message)

// websocketWs.send(message)

// 将文件内容发送回渲染进程

// event.sender.send('export-response', excelData);

});

//java

// cmdHomePath = getExecuteResourcesPath('/');

cmdPath = getExecuteResourcesPath('jre/bin/java');

cmdParam = getResourcesPath('java/usefullc-mp-bootstrap.jar');

exec_system_file(cmdPath,['-jar',cmdParam],function(message){

console.log(message)

// websocketWs.send(message)

// 将文件内容发送回渲染进程

// event.sender.send('export-response', excelData);

});

function getOsPlatform(){

let osPlatform = process.platform == 'darwin' ? 'mac' : 'win';

return osPlatform;

}

function getExecuteResourcesPath(filePath){

let osPlatform = getOsPlatform();

let executeFilePath = path.join(__dirname, 'resources/'+osPlatform+"/"+filePath);

return executeFilePath;

}

function getResourcesPath(filePath){

let executeFilePath = path.join(__dirname, 'resources/'+filePath);

return executeFilePath;

}

function exec_system_file(executeFilePath,params,callback){

// params = params || {}

// Python可执行文件的路径(根据您的项目结构进行调整)

// let osPlatform = process.platform == 'darwin' ? 'mac' : 'win';

// let pythonExecutable = path.join(__dirname, 'resources/'+osPlatform+"/"+filePath);

// 要传递给Python脚本的参数(如果需要)

let pythonArgs = [];

if (params){

pythonArgs = pythonArgs.concat(params)

// pythonArgs = [params.startDate, params.endDate];

}

console.log('executeFilePath,params',executeFilePath,pythonArgs);

// 启动Python脚本

const child = execFile(executeFilePath, pythonArgs, (error, stdout, stderr) => {

if (error) {

console.error(`执行脚本时出错: ${error}`);

// mainWindow.webContents.send('query-result', error);

return;

}

if(callback){

callback(stdout)

}

// mainWindow.webContents.send('query-result', stdout);

// 标准输出(stdout)包含Python脚本的执行结果

console.log(`脚本执行结果: ${stdout}`);

// 如果有错误输出(stderr),可以在这里处理

if (stderr) {

console.error(`脚本错误输出: ${stderr}`);

// mainWindow.webContents.send('query-result', stderr);

}

});

// 监听Python脚本的退出事件

child.on('exit', (code) => {

console.log(`脚本退出,退出码: ${code}`);

// mainWindow.webContents.send('query-result', code);

});

childProcesses.push(child)

}

资源文件结构如下图所示,所有的资源文件都是压缩版,并且已配置好。(如需下载见附件)

相关链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: