Skip to content

Conversation

@CiiLu
Copy link
Contributor

@CiiLu CiiLu commented Jan 20, 2026

突然发现不光运行路径,Jar路径也不能包括 !,否则也会报错

Exception in thread "main" java.lang.NullPointerException
        at java.base/java.io.Reader.<init>(Reader.java:286)
        at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:119)
        at org.jackhuang.hmcl.util.SelfDependencyPatcher$DependencyDescriptor.readDependencies(SelfDependencyPatcher.java:108)
        at org.jackhuang.hmcl.util.SelfDependencyPatcher.<init>(SelfDependencyPatcher.java:76)
        at org.jackhuang.hmcl.util.SelfDependencyPatcher.patch(SelfDependencyPatcher.java:176)
        at org.jackhuang.hmcl.EntryPoint.checkJavaFX(EntryPoint.java:207)
        at org.jackhuang.hmcl.EntryPoint.main(EntryPoint.java:58)
        at org.jackhuang.hmcl.Main.main(Main.java:107)

之前运行路径的检查也需要保留,否则 JavaFX 可能会无法加载 css 样式


private static void checkDirectoryPath() {
String currentDir = System.getProperty("user.dir", "");
String thisJarPath = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请检查 CodeSource 是否为 null

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an additional startup validation to prevent launching HMCL from locations that contain !, extending the existing working-directory check to also cover the HMCL jar path to avoid downstream runtime errors.

Changes:

  • Read the HMCL jar path from Main.class CodeSource and check whether it contains !.
  • Show an error dialog and exit when the jar path contains ! (in addition to the existing working-directory check).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


private static void checkDirectoryPath() {
String currentDir = System.getProperty("user.dir", "");
String thisJarPath = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main.class.getProtectionDomain().getCodeSource().getLocation() can be null in some classloading scenarios; calling .getPath() unconditionally introduces a new potential NPE during startup. Consider retrieving the CodeSource/Location with null + SecurityException handling (and only computing the jar path after the working-dir check) so checkDirectoryPath() never crashes before showing an error dialog.

Copilot uses AI. Check for mistakes.
SwingUtils.initLookAndFeel();
System.err.println("The jar path contains an exclamation mark: " + thisJarPath);
// 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);
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the jar-path ! branch, the dialog prints currentDir instead of thisJarPath, which will mislead users about which path is invalid. Use the jar path variable (and consider aligning the wording with the stderr message).

Suggested change
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 jar path.\n" + "The path is " + thisJarPath);

Copilot uses AI. Check for mistakes.
Comment on lines 105 to 107
private static void checkDirectoryPath() {
String currentDir = System.getProperty("user.dir", "");
String thisJarPath = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new jar-path validation is startup-critical but currently hard to test because checkDirectoryPath() is private and exits the JVM/UI-calls directly. Consider extracting the core validation into a package-private pure method (e.g., taking currentDir and jarPath and returning an optional error) and add unit tests for both working-dir and jar-path cases.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants