electron 嵌入java应用,并且嵌入redis,mysql,打包

本文介绍了如何在使用Electron开发的桌面应用中嵌入Java应用,包括配置JRE1.8、Redis(支持Win32和MacOSX)、MySQL8,并提供打包不同平台版本的步骤,以及启动依赖进程的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)
}

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程答疑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值