From 05251cb4e35be01e9d19e45f64780c3637212edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=9E=E5=BA=90?= <109708109+CiiLu@users.noreply.github.com> Date: Sun, 1 Feb 2026 17:17:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=97=B6=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=20Jar=20=E8=B7=AF=E5=BE=84=E6=98=AF=E5=90=A6=E5=8C=85=E5=90=AB?= =?UTF-8?q?=20`!`=20(#5272)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/org/jackhuang/hmcl/Main.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/HMCLBoot/src/main/java/org/jackhuang/hmcl/Main.java b/HMCLBoot/src/main/java/org/jackhuang/hmcl/Main.java index 457dbe814..adc1f208b 100644 --- a/HMCLBoot/src/main/java/org/jackhuang/hmcl/Main.java +++ b/HMCLBoot/src/main/java/org/jackhuang/hmcl/Main.java @@ -20,6 +20,11 @@ package org.jackhuang.hmcl; import org.jackhuang.hmcl.util.SwingUtils; import javax.swing.*; +import java.net.URISyntaxException; +import java.nio.file.FileSystemNotFoundException; +import java.nio.file.Paths; +import java.security.CodeSource; +import java.security.ProtectionDomain; import java.util.ResourceBundle; /** @@ -104,12 +109,35 @@ public final class Main { private static void checkDirectoryPath() { String currentDir = System.getProperty("user.dir", ""); + String jarPath = getThisJarPath(); if (currentDir.contains("!")) { SwingUtils.initLookAndFeel(); System.err.println("The current working path contains an exclamation mark: " + currentDir); // No Chinese translation because both Swing and JavaFX cannot render Chinese character properly when exclamation mark exists in the path. - SwingUtils.showErrorDialog("Exclamation mark(!) is not allowed in the path where HMCL is in.\n" + "The path is " + currentDir); + SwingUtils.showErrorDialog("Exclamation mark(!) is not allowed in the working path.\n" + "The path is " + currentDir); System.exit(1); + } else if (jarPath != null && jarPath.contains("!")) { + SwingUtils.initLookAndFeel(); + System.err.println("The jar path contains an exclamation mark: " + jarPath); + // No Chinese translation because both Swing and JavaFX cannot render Chinese character properly when exclamation mark exists in the path. + SwingUtils.showErrorDialog("Exclamation mark(!) is not allowed in the path where HMCL is in.\n" + "The path is " + jarPath); + System.exit(1); + } + } + + private static String getThisJarPath() { + ProtectionDomain protectionDomain = Main.class.getProtectionDomain(); + if (protectionDomain == null) + return null; + + CodeSource codeSource = protectionDomain.getCodeSource(); + if (codeSource == null || codeSource.getLocation() == null) + return null; + + try { + return Paths.get(codeSource.getLocation().toURI()).toAbsolutePath().normalize().toString(); + } catch (FileSystemNotFoundException | IllegalArgumentException | URISyntaxException e) { + return null; } }