2025-06-04 17:27:37 +08:00
|
|
|
|
import os
|
2025-06-05 09:05:40 +08:00
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
import elevate
|
2025-06-04 17:27:37 +08:00
|
|
|
|
from loguru import logger
|
|
|
|
|
|
import subprocess
|
|
|
|
|
|
import sys
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
import re
|
2025-06-05 09:05:40 +08:00
|
|
|
|
import webview
|
2025-06-04 17:27:37 +08:00
|
|
|
|
|
|
|
|
|
|
# 获取当前脚本所在的目录
|
|
|
|
|
|
new_java_path = 'D:/database/jdk/bin/java.exe'
|
2025-06-05 09:05:40 +08:00
|
|
|
|
port = 12396
|
2025-06-04 17:27:37 +08:00
|
|
|
|
def get_resource_path(relative_path):
|
|
|
|
|
|
""" 获取资源绝对路径,适用于开发环境和PyInstaller打包后 """
|
|
|
|
|
|
if hasattr(sys, '_MEIPASS'):
|
|
|
|
|
|
# 打包后的资源路径:sys._MEIPASS + 相对路径
|
|
|
|
|
|
base_path = sys._MEIPASS
|
|
|
|
|
|
else:
|
|
|
|
|
|
# 开发环境的基础路径
|
|
|
|
|
|
base_path = os.path.dirname(os.path.abspath("."))
|
|
|
|
|
|
return os.path.join(base_path, relative_path)
|
|
|
|
|
|
|
|
|
|
|
|
# 构建JDK和JAR的路径
|
|
|
|
|
|
jar_path = os.path.join("datas", "electromagnetic.jar")
|
|
|
|
|
|
|
|
|
|
|
|
def start():
|
|
|
|
|
|
try:
|
|
|
|
|
|
new_jar_path = get_resource_path(jar_path)
|
|
|
|
|
|
java_command = [new_java_path, "-jar", new_jar_path]
|
|
|
|
|
|
# 创建新进程组(支持Unix/Windows的进程隔离)
|
|
|
|
|
|
creation_flags = subprocess.CREATE_NEW_PROCESS_GROUP
|
|
|
|
|
|
# 重定向输出到日志文件
|
|
|
|
|
|
formatted_time = datetime.now().strftime("%Y%m%d%H%M%S")
|
|
|
|
|
|
log_file = rf'D:\database\app_{formatted_time}.log'
|
|
|
|
|
|
with open(log_file, "a") as log:
|
|
|
|
|
|
process = subprocess.Popen(
|
|
|
|
|
|
java_command,
|
|
|
|
|
|
stdout=log,
|
|
|
|
|
|
stderr=log,
|
|
|
|
|
|
start_new_session=True, # 创建新会话
|
|
|
|
|
|
creationflags=creation_flags
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
logger.info(f"Spring Boot 应用已在后台启动! PID: {process.pid}")
|
|
|
|
|
|
logger.info(f"日志输出: {os.path.abspath(log_file)}")
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.info(f"启动失败: {str(e)}")
|
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def kill_process_by_port(port):
|
|
|
|
|
|
# 执行netstat命令获取端口占用信息
|
|
|
|
|
|
cmd_netstat = ['netstat', '-ano', '-p', 'tcp']
|
|
|
|
|
|
result = subprocess.run(cmd_netstat, capture_output=True, text=True, shell=True)
|
|
|
|
|
|
|
|
|
|
|
|
# 在输出中查找指定端口
|
|
|
|
|
|
pid = None
|
|
|
|
|
|
pattern = fr':{port}\s+.*LISTENING\s+(\d+)'
|
|
|
|
|
|
match = re.search(pattern, result.stdout)
|
|
|
|
|
|
if match:
|
|
|
|
|
|
pid = match.group(1)
|
|
|
|
|
|
|
|
|
|
|
|
# 如果找到PID则终止进程
|
|
|
|
|
|
if pid:
|
|
|
|
|
|
try:
|
|
|
|
|
|
subprocess.run(['taskkill', '/F', '/PID', pid], check=True)
|
|
|
|
|
|
logger.info(f"已终止占用端口 {port} 的进程 (PID: {pid})")
|
|
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
|
|
logger.info(f"终止进程 {pid} 失败 (可能权限不足或进程不存在)")
|
|
|
|
|
|
else:
|
|
|
|
|
|
logger.info(f"端口 {port} 未被占用")
|
|
|
|
|
|
|
2025-06-05 09:05:40 +08:00
|
|
|
|
def open_web():
|
|
|
|
|
|
webview.settings['ALLOW_DOWNLOADS'] = True
|
|
|
|
|
|
webview.settings['OPEN_DEVTOOLS_IN_DEBUG'] = False
|
|
|
|
|
|
webview.create_window('数据库管理系统', 'http://127.0.0.1:12396/index')
|
|
|
|
|
|
webview.start(debug=True)
|
|
|
|
|
|
|
2025-06-04 17:27:37 +08:00
|
|
|
|
if __name__ == '__main__':
|
2025-06-05 09:05:40 +08:00
|
|
|
|
elevate.elevate()
|
|
|
|
|
|
kill_process_by_port(port)
|
|
|
|
|
|
start()
|
|
|
|
|
|
time.sleep(10)
|
|
|
|
|
|
open_web()
|