mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-09 11:55:52 -04:00
perf: cache Java version from executable.
This commit is contained in:
parent
94b2cafa7a
commit
c3f19d2c4a
@ -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<Path, JavaVersion> 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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user