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 c18310be6..c2cafc864 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 @@ -30,6 +30,8 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.logging.Level; import java.util.regex.Matcher; @@ -108,12 +110,17 @@ public final class JavaVersion { return UNKNOWN; } + private static final Map fromExecutableCache = new ConcurrentHashMap<>(); + public static JavaVersion fromExecutable(Path executable) throws IOException { + executable = executable.toRealPath(); + JavaVersion cachedJavaVersion = fromExecutableCache.get(executable); + if (cachedJavaVersion != null) + return cachedJavaVersion; + Platform platform = Platform.BIT_32; String version = null; - executable = executable.toRealPath(); - Process process = new ProcessBuilder(executable.toString(), "-version").start(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) { for (String line; (line = reader.readLine()) != null;) { @@ -130,8 +137,9 @@ public final class JavaVersion { if (parseVersion(version) == UNKNOWN) throw new IOException("Unrecognized Java version " + version); - - return new JavaVersion(executable, version, platform); + JavaVersion javaVersion = new JavaVersion(executable, version, platform); + fromExecutableCache.put(executable, javaVersion); + return javaVersion; } private static Path getExecutable(Path javaHome) {