使用低版本 Java 启动且带有 --apply-to 参数时提醒用户手动下载最新版本 HMCL (#4793)

This commit is contained in:
Glavo
2025-11-17 20:23:40 +08:00
committed by GitHub
parent 458793f0c0
commit 02bd9e9d2d
6 changed files with 153 additions and 7 deletions

View File

@@ -0,0 +1,99 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl;
import java.io.File;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Locale;
/// @author Glavo
public final class DesktopUtils {
private static final String[] linuxBrowsers = {
"xdg-open",
"google-chrome",
"firefox",
"microsoft-edge",
"opera",
"konqueror",
"mozilla"
};
public static Path which(String command) {
String path = System.getenv("PATH");
if (path == null)
return null;
try {
for (String item : path.split(File.pathSeparator)) {
try {
Path program = Paths.get(item, command);
if (Files.isExecutable(program))
return program.toRealPath();
} catch (Throwable ignored) {
}
}
} catch (Throwable ignored) {
}
return null;
}
public static void openLink(String link) {
String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT);
Throwable e1 = null;
try {
if (osName.startsWith("windows")) {
Runtime.getRuntime().exec(new String[]{"rundll32.exe", "url.dll,FileProtocolHandler", link});
return;
} else if (osName.startsWith("mac") || osName.startsWith("darwin")) {
Runtime.getRuntime().exec(new String[]{"open", link});
return;
} else {
for (String browser : linuxBrowsers) {
Path path = which(browser);
if (path != null) {
try {
Runtime.getRuntime().exec(new String[]{path.toString(), link});
return;
} catch (Throwable ignored) {
}
}
}
System.err.println("No known browser found");
}
} catch (Throwable e) {
e1 = e;
}
try {
java.awt.Desktop.getDesktop().browse(new URI(link));
} catch (Throwable e2) {
if (e1 != null)
e2.addSuppressed(e1);
e2.printStackTrace(System.err);
}
}
private DesktopUtils() {
}
}

View File

@@ -19,11 +19,15 @@ package org.jackhuang.hmcl;
import org.jackhuang.hmcl.util.SwingUtils;
import javax.swing.*;
import java.util.ResourceBundle;
/**
* @author Glavo
*/
public final class Main {
private static final int MINIMUM_JAVA_VERSION = 17;
private static final String DOWNLOAD_PAGE = "https://hmcl.huangyuhui.net/download/";
private Main() {
}
@@ -73,14 +77,36 @@ public final class Main {
}
}
static void showErrorAndExit(String[] args) {
SwingUtils.initLookAndFeel();
ResourceBundle resourceBundle = BootProperties.getResourceBundle();
String errorTitle = resourceBundle.getString("boot.message.error");
if (args.length > 0 && args[0].equals("--apply-to")) {
String errorMessage = resourceBundle.getString("boot.manual_update");
System.err.println(errorMessage);
int result = JOptionPane.showOptionDialog(null, errorMessage, errorTitle, JOptionPane.YES_NO_OPTION,
JOptionPane.ERROR_MESSAGE, null, null, null);
if (result == JOptionPane.YES_OPTION) {
System.out.println("Open " + DOWNLOAD_PAGE);
DesktopUtils.openLink(DOWNLOAD_PAGE);
}
} else {
String errorMessage = resourceBundle.getString("boot.unsupported_java_version");
System.err.println(errorMessage);
SwingUtils.showErrorDialog(errorMessage, errorTitle);
}
System.exit(1);
}
public static void main(String[] args) throws Throwable {
if (getJavaFeatureVersion(System.getProperty("java.version")) >= MINIMUM_JAVA_VERSION) {
EntryPoint.main(args);
} else {
String errorMessage = BootProperties.getResourceBundle().getString("boot.unsupported_java_version");
System.err.println(errorMessage);
SwingUtils.showErrorDialog(errorMessage);
System.exit(1);
showErrorAndExit(args);
}
}
}

View File

@@ -40,14 +40,26 @@ public final class SwingUtils {
}
public static void showInfoDialog(Object message) {
JOptionPane.showMessageDialog(null, message, "Info", JOptionPane.INFORMATION_MESSAGE);
showInfoDialog(message, "Info");
}
public static void showInfoDialog(Object message, String title) {
JOptionPane.showMessageDialog(null, message, title, JOptionPane.INFORMATION_MESSAGE);
}
public static void showWarningDialog(Object message) {
JOptionPane.showMessageDialog(null, message, "Warning", JOptionPane.WARNING_MESSAGE);
showWarningDialog(message, "Warning");
}
public static void showWarningDialog(Object message, String title) {
JOptionPane.showMessageDialog(null, message, title, JOptionPane.WARNING_MESSAGE);
}
public static void showErrorDialog(Object message) {
JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
showErrorDialog(message, "Error");
}
public static void showErrorDialog(Object message, String title) {
JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
}
}

View File

@@ -17,3 +17,6 @@
#
boot.unsupported_java_version=HMCL requires Java 17 or later to run, but still supports launching games with Java 6~16. Please install the latest version of Java and try opening HMCL again.\nYou can keep your old version of Java. HMCL can detect and manage multiple Java installations, and will automatically select the appropriate Java version for your game.
boot.manual_update=HMCL cannot complete automatic updates in the current environment. Please download the latest version of HMCL manually.\nWould you like to go to the download page?
boot.message.error=Error

View File

@@ -17,3 +17,6 @@
#
boot.unsupported_java_version=HMCL 需要 Java 17 或更高版本才能运行,但依然支持使用 Java 6~16 启动游戏。请安装最新版本的 Java 再尝试启动 HMCL。\n你可以继续保留旧版本 Java。HMCL 能够识别与管理多个 Java并会自动根据游戏版本为你选择合适的 Java。\n你可以访问 https://docs.hmcl.net/help.html 页面寻求帮助。
boot.manual_update=HMCL 在当前环境无法完成自动更新,请手动下载最新版本的 HMCL。\n是否前往下载页面
boot.message.error=错误

View File

@@ -17,3 +17,6 @@
#
boot.unsupported_java_version=HMCL 需要 Java 17 或更高版本才能執行,但依然支援使用 Java 6~16 啟動遊戲。請安裝最新版本的 Java 再嘗試開啟 HMCL。\n你可以繼續保留舊版本 Java。HMCL 能夠識別與管理多個 Java並會自動根據遊戲版本為你選取合適的 Java。
boot.manual_update=HMCL 在當前環境無法完成自動更新,請手動下載最新版本的 HMCL。\n是否前往下載頁面
boot.message.error=錯誤