From 76ef47efc4dd64c59c93078a47a25279480c9d65 Mon Sep 17 00:00:00 2001 From: huanghongxun Date: Sun, 30 Sep 2018 13:22:42 +0800 Subject: [PATCH] Suppress exceptions thrown when checking Java installation --- .../hmcl/util/platform/JavaVersion.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/JavaVersion.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/JavaVersion.java index 6f926b721..236567798 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/JavaVersion.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/JavaVersion.java @@ -28,6 +28,7 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.concurrent.CountDownLatch; import java.util.logging.Level; import java.util.regex.Matcher; @@ -231,18 +232,28 @@ public final class JavaVersion { } // ==== + private static JavaVersion fromJavaHomeQuietly(File home) { + try { + return fromJavaHome(home); + } catch (IOException e) { + Logging.LOG.log(Level.WARNING, "Couldn't determine java " + home, e); + return null; + } + } + // ==== OSX ==== - private static List queryMacintosh() throws IOException { + private static List queryMacintosh() { List res = new ArrayList<>(); File currentJRE = new File("/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home"); if (currentJRE.exists()) - res.add(fromJavaHome(currentJRE)); + res.add(fromJavaHomeQuietly(currentJRE)); File[] files = new File("/Library/Java/JavaVirtualMachines/").listFiles(); if (files != null) for (File file : files) - res.add(fromJavaHome(new File(file, "Contents/Home"))); + res.add(fromJavaHomeQuietly(new File(file, "Contents/Home"))); + res.removeIf(Objects::isNull); return res; } // ==== @@ -263,8 +274,10 @@ public final class JavaVersion { if (!querySubFolders(java).contains(java + "\\MSI")) continue; String home = queryRegisterValue(java, "JavaHome"); if (home != null) - res.add(fromJavaHome(new File(home))); + res.add(fromJavaHomeQuietly(new File(home))); } + + res.removeIf(Objects::isNull); return res; }