mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-18 00:10:33 -04:00
Refactor JavaVersion
This commit is contained in:
parent
6777dd7ea7
commit
8e6bcd969e
@ -38,7 +38,7 @@ import java.util.stream.Stream;
|
|||||||
*
|
*
|
||||||
* @author huangyuhui
|
* @author huangyuhui
|
||||||
*/
|
*/
|
||||||
public final class JavaVersion implements Serializable {
|
public final class JavaVersion {
|
||||||
|
|
||||||
private final File binary;
|
private final File binary;
|
||||||
private final String longVersion;
|
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_9
|
||||||
* @see org.jackhuang.hmcl.util.JavaVersion#JAVA_8
|
* @see org.jackhuang.hmcl.util.JavaVersion#JAVA_8
|
||||||
* @see org.jackhuang.hmcl.util.JavaVersion#JAVA_7
|
* @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
|
* @see org.jackhuang.hmcl.util.JavaVersion#UNKNOWN
|
||||||
*/
|
*/
|
||||||
public int getParsedVersion() {
|
public int getParsedVersion() {
|
||||||
@ -82,52 +80,44 @@ public final class JavaVersion implements Serializable {
|
|||||||
private static final Pattern REGEX = Pattern.compile("version \"(?<version>(.*?))\"");
|
private static final Pattern REGEX = Pattern.compile("version \"(?<version>(.*?))\"");
|
||||||
|
|
||||||
public static final int UNKNOWN = -1;
|
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_7 = 70;
|
||||||
public static final int JAVA_8 = 80;
|
public static final int JAVA_8 = 80;
|
||||||
public static final int JAVA_9 = 90;
|
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) {
|
private static int parseVersion(String version) {
|
||||||
if (version.startsWith("10") || version.startsWith("X"))
|
if (version.startsWith("11"))
|
||||||
return JAVA_X;
|
return JAVA_11;
|
||||||
else if (version.contains("1.9.") || version.startsWith("9"))
|
else if (version.startsWith("10"))
|
||||||
|
return JAVA_10;
|
||||||
|
else if (version.startsWith("9"))
|
||||||
return JAVA_9;
|
return JAVA_9;
|
||||||
else if (version.contains("1.8"))
|
else if (version.contains("1.8"))
|
||||||
return JAVA_8;
|
return JAVA_8;
|
||||||
else if (version.contains("1.7"))
|
else if (version.contains("1.7"))
|
||||||
return JAVA_7;
|
return JAVA_7;
|
||||||
else if (version.contains("1.6"))
|
|
||||||
return JAVA_6;
|
|
||||||
else if (version.contains("1.5"))
|
|
||||||
return JAVA_5;
|
|
||||||
else
|
else
|
||||||
return UNKNOWN;
|
return UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JavaVersion fromExecutable(File executable) throws IOException {
|
public static JavaVersion fromExecutable(File executable) throws IOException {
|
||||||
File actualFile = executable;
|
|
||||||
Platform platform = Platform.BIT_32;
|
Platform platform = Platform.BIT_32;
|
||||||
String version = null;
|
String version = null;
|
||||||
|
|
||||||
if ("javaw".equals(FileUtils.getNameWithoutExtension(actualFile)))
|
// javaw is only used on windows
|
||||||
actualFile = new File(actualFile.getAbsoluteFile().getParentFile(), "java");
|
if ("javaw.exe".equalsIgnoreCase(executable.getName()))
|
||||||
|
executable = new File(executable.getAbsoluteFile().getParentFile(), "java.exe");
|
||||||
|
|
||||||
try {
|
Process process = new ProcessBuilder(executable.getAbsolutePath(), "-version").start();
|
||||||
Process process = new ProcessBuilder(actualFile.getAbsolutePath(), "-version").start();
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
|
||||||
process.waitFor();
|
for (String line; (line = reader.readLine()) != null;) {
|
||||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
|
Matcher m = REGEX.matcher(line);
|
||||||
for (String line; (line = reader.readLine()) != null; ) {
|
if (m.find())
|
||||||
Matcher m = REGEX.matcher(line);
|
version = m.group("version");
|
||||||
if (m.find())
|
if (line.contains("64-Bit"))
|
||||||
version = m.group("version");
|
platform = Platform.BIT_64;
|
||||||
if (line.contains("64-Bit"))
|
|
||||||
platform = Platform.BIT_64;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new IOException("Interrupted scanning the java version.", e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (version == null)
|
if (version == null)
|
||||||
@ -135,24 +125,20 @@ public final class JavaVersion implements Serializable {
|
|||||||
|
|
||||||
if (parseVersion(version) == UNKNOWN)
|
if (parseVersion(version) == UNKNOWN)
|
||||||
throw new IOException("Unrecognized Java version " + version);
|
throw new IOException("Unrecognized Java version " + version);
|
||||||
return new JavaVersion(actualFile, version, platform);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static JavaVersion fromKnownExecutable(File file, String version) {
|
return new JavaVersion(executable, version, platform);
|
||||||
return new JavaVersion(file, version, Platform.UNKNOWN);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JavaVersion fromJavaHome(File home) throws IOException {
|
public static JavaVersion fromJavaHome(File home) throws IOException {
|
||||||
return fromExecutable(getJavaFile(home));
|
return fromExecutable(getExecutable(home));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static File getJavaFile(File home) {
|
private static File getExecutable(File javaHome) {
|
||||||
File path = new File(home, "bin");
|
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) {
|
||||||
File javaw = new File(path, "javae.exe");
|
return new File(javaHome, "bin/java.exe");
|
||||||
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS && javaw.isFile())
|
} else {
|
||||||
return javaw;
|
return new File(javaHome, "bin/java");
|
||||||
else
|
}
|
||||||
return new File(path, "java"); // Both linux and windows allow this.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JavaVersion fromCurrentEnvironment() {
|
public static JavaVersion fromCurrentEnvironment() {
|
||||||
@ -160,7 +146,7 @@ public final class JavaVersion implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static final JavaVersion THIS_JAVA = new JavaVersion(
|
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"),
|
System.getProperty("java.version"),
|
||||||
Platform.PLATFORM
|
Platform.PLATFORM
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user