From 729b2e6219d799341839d5e6e15d92b86257f296 Mon Sep 17 00:00:00 2001 From: "583641232@qq.com" <583641232@qq.com> Date: Sat, 6 Jul 2024 10:34:14 +0800 Subject: [PATCH] :sparkles: go --- main.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/main.js b/main.js index f72e3e1..ed0908c 100644 --- a/main.js +++ b/main.js @@ -1,5 +1,6 @@ // Modules to control application life and create native browser window const { app, BrowserWindow } = require('electron') +const { exec } = require('child_process'); const path = require('node:path') function createWindow () { @@ -45,5 +46,30 @@ app.on('window-all-closed', function () { if (process.platform !== 'darwin') app.quit() }) +app.on('before-quit', () => { + getJavaProcessIdByPort(30523); +}); + + + +function getJavaProcessIdByPort(port) { + let pid = null; + exec(`netstat -ano | findstr :${port}`, (error, stdout, stderr) => { + // 查找stdout中匹配端口的行,并提取PID + if (stdout) { + const lines = stdout.trim().split('\r\n'); + for (const line of lines) { + const pidMatch = line.match(/LISTENING\s+(\d+)/);; + if (pidMatch) { + pid = pidMatch[1]; + require('child_process').execSync(`taskkill /PID ${pid} -t -f`); + return; + } + } + } + }); +} + + // In this file you can include the rest of your app's specific main process // code. You can also put them in separate files and require them here.