Merge pull request #339 from yushijinhun/multiple-java
Linux下自动检测Java + 允许多个相同版本的JRE
This commit is contained in:
@@ -18,10 +18,17 @@
|
||||
package org.jackhuang.hmcl.util;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.LinkOption;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.logging.Level;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Represents a Java installation.
|
||||
@@ -69,7 +76,7 @@ public final class JavaVersion implements Serializable {
|
||||
return version;
|
||||
}
|
||||
|
||||
private static final Pattern REGEX = Pattern.compile("java version \"(?<version>(.*?))\"");
|
||||
private static final Pattern REGEX = Pattern.compile("version \"(?<version>(.*?))\"");
|
||||
|
||||
public static final int UNKNOWN = -1;
|
||||
public static final int JAVA_5 = 50;
|
||||
@@ -155,10 +162,10 @@ public final class JavaVersion implements Serializable {
|
||||
Platform.PLATFORM
|
||||
);
|
||||
|
||||
private static Map<String, JavaVersion> JAVAS;
|
||||
private static List<JavaVersion> JAVAS;
|
||||
private static final CountDownLatch LATCH = new CountDownLatch(1);
|
||||
|
||||
public static Map<String, JavaVersion> getJREs() throws InterruptedException {
|
||||
public static List<JavaVersion> getJREs() throws InterruptedException {
|
||||
if (JAVAS != null)
|
||||
return JAVAS;
|
||||
LATCH.await();
|
||||
@@ -168,13 +175,14 @@ public final class JavaVersion implements Serializable {
|
||||
public static synchronized void initialize() throws IOException {
|
||||
if (JAVAS != null)
|
||||
throw new IllegalStateException("JavaVersions have already been initialized.");
|
||||
HashMap<String, JavaVersion> temp = new HashMap<>();
|
||||
temp.put(THIS_JAVA.getVersion(), THIS_JAVA);
|
||||
List<JavaVersion> javaVersions;
|
||||
switch (OperatingSystem.CURRENT_OS) {
|
||||
case WINDOWS:
|
||||
javaVersions = queryWindows();
|
||||
break;
|
||||
case LINUX:
|
||||
javaVersions = queryLinux();
|
||||
break;
|
||||
case OSX:
|
||||
javaVersions = queryMacintosh();
|
||||
break;
|
||||
@@ -182,12 +190,31 @@ public final class JavaVersion implements Serializable {
|
||||
javaVersions = Collections.emptyList();
|
||||
break;
|
||||
}
|
||||
for (JavaVersion v : javaVersions)
|
||||
temp.put(v.getVersion(), v);
|
||||
JAVAS = Collections.unmodifiableMap(temp);
|
||||
JAVAS = Collections.unmodifiableList(javaVersions);
|
||||
LATCH.countDown();
|
||||
}
|
||||
|
||||
private static List<JavaVersion> queryLinux() throws IOException {
|
||||
Path jvmDir = Paths.get("/usr/lib/jvm");
|
||||
if (Files.isDirectory(jvmDir)) {
|
||||
return Files.list(jvmDir)
|
||||
.filter(dir -> Files.isDirectory(dir, LinkOption.NOFOLLOW_LINKS))
|
||||
.map(dir -> dir.resolve("bin/java"))
|
||||
.filter(Files::isExecutable)
|
||||
.flatMap(executable -> {
|
||||
try {
|
||||
return Stream.of(fromExecutable(executable.toFile()));
|
||||
} catch (IOException e) {
|
||||
Logging.LOG.log(Level.WARNING, "Couldn't determine java " + executable, e);
|
||||
return Stream.empty();
|
||||
}
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
private static List<JavaVersion> queryMacintosh() throws IOException {
|
||||
LinkedList<JavaVersion> res = new LinkedList<>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user