2
0
mirror of https://github.com/HMCL-dev/HMCL.git synced 2025-02-23 17:19:44 +08:00

Refactor JavaVersion

This commit is contained in:
yushijinhun 2018-08-09 12:51:32 +08:00
parent 6777dd7ea7
commit 8e6bcd969e
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4

View File

@ -38,7 +38,7 @@ import java.util.stream.Stream;
*
* @author huangyuhui
*/
public final class JavaVersion implements Serializable {
public final class JavaVersion {
private final File binary;
private final String longVersion;
@ -71,8 +71,6 @@ public final class JavaVersion implements Serializable {
* @see org.jackhuang.hmcl.util.JavaVersion#JAVA_9
* @see org.jackhuang.hmcl.util.JavaVersion#JAVA_8
* @see org.jackhuang.hmcl.util.JavaVersion#JAVA_7
* @see org.jackhuang.hmcl.util.JavaVersion#JAVA_6
* @see org.jackhuang.hmcl.util.JavaVersion#JAVA_5
* @see org.jackhuang.hmcl.util.JavaVersion#UNKNOWN
*/
public int getParsedVersion() {
@ -82,52 +80,44 @@ public final class JavaVersion implements Serializable {
private static final Pattern REGEX = Pattern.compile("version \"(?<version>(.*?))\"");
public static final int UNKNOWN = -1;
public static final int JAVA_5 = 50;
public static final int JAVA_6 = 60;
public static final int JAVA_7 = 70;
public static final int JAVA_8 = 80;
public static final int JAVA_9 = 90;
public static final int JAVA_X = 100;
public static final int JAVA_10 = 100;
public static final int JAVA_11 = 110;
private static int parseVersion(String version) {
if (version.startsWith("10") || version.startsWith("X"))
return JAVA_X;
else if (version.contains("1.9.") || version.startsWith("9"))
if (version.startsWith("11"))
return JAVA_11;
else if (version.startsWith("10"))
return JAVA_10;
else if (version.startsWith("9"))
return JAVA_9;
else if (version.contains("1.8"))
return JAVA_8;
else if (version.contains("1.7"))
return JAVA_7;
else if (version.contains("1.6"))
return JAVA_6;
else if (version.contains("1.5"))
return JAVA_5;
else
return UNKNOWN;
}
public static JavaVersion fromExecutable(File executable) throws IOException {
File actualFile = executable;
Platform platform = Platform.BIT_32;
String version = null;
if ("javaw".equals(FileUtils.getNameWithoutExtension(actualFile)))
actualFile = new File(actualFile.getAbsoluteFile().getParentFile(), "java");
// javaw is only used on windows
if ("javaw.exe".equalsIgnoreCase(executable.getName()))
executable = new File(executable.getAbsoluteFile().getParentFile(), "java.exe");
try {
Process process = new ProcessBuilder(actualFile.getAbsolutePath(), "-version").start();
process.waitFor();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
for (String line; (line = reader.readLine()) != null; ) {
Matcher m = REGEX.matcher(line);
if (m.find())
version = m.group("version");
if (line.contains("64-Bit"))
platform = Platform.BIT_64;
}
Process process = new ProcessBuilder(executable.getAbsolutePath(), "-version").start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
for (String line; (line = reader.readLine()) != null;) {
Matcher m = REGEX.matcher(line);
if (m.find())
version = m.group("version");
if (line.contains("64-Bit"))
platform = Platform.BIT_64;
}
} catch (InterruptedException e) {
throw new IOException("Interrupted scanning the java version.", e);
}
if (version == null)
@ -135,24 +125,20 @@ public final class JavaVersion implements Serializable {
if (parseVersion(version) == UNKNOWN)
throw new IOException("Unrecognized Java version " + version);
return new JavaVersion(actualFile, version, platform);
}
private static JavaVersion fromKnownExecutable(File file, String version) {
return new JavaVersion(file, version, Platform.UNKNOWN);
return new JavaVersion(executable, version, platform);
}
public static JavaVersion fromJavaHome(File home) throws IOException {
return fromExecutable(getJavaFile(home));
return fromExecutable(getExecutable(home));
}
private static File getJavaFile(File home) {
File path = new File(home, "bin");
File javaw = new File(path, "javae.exe");
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS && javaw.isFile())
return javaw;
else
return new File(path, "java"); // Both linux and windows allow this.
private static File getExecutable(File javaHome) {
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) {
return new File(javaHome, "bin/java.exe");
} else {
return new File(javaHome, "bin/java");
}
}
public static JavaVersion fromCurrentEnvironment() {
@ -160,7 +146,7 @@ public final class JavaVersion implements Serializable {
}
public static final JavaVersion THIS_JAVA = new JavaVersion(
getJavaFile(new File(System.getProperty("java.home"))),
getExecutable(new File(System.getProperty("java.home"))),
System.getProperty("java.version"),
Platform.PLATFORM
);