由于 Windows 可靠地将其版本信息报告为float
,因此可以通过了解每个 Windows 版本并进行比较来计算“XP 或更高版本”。
速记:
// Is this XP or higher?
// Well, XP Reports as 5.1 (32-bit) or 5.2 (64-bit). All higher OSs use a higher version
final boolean XP_OR_HIGHER = (Float.parseFloat(System.getProperty("os.version")) >= 5.1f);
但是由于解析系统值可能引发异常,再加上 XP 是 EOL 的事实,并且更有可能有人正在寻找更新的操作系统比较......这是一种更全面的方法。
注意:对于 Windows 10,所有版本都报告为10.0
无论 Microsoft 在版本之间发生了多少变化。如需更详细的信息,您必须查看注册表(另请参阅HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ReleaseId
)。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class WinVer {
enum WindowsType {
/*
* Keep descending for best match, or rewrite using comparitor
*/
WINDOWS_10(10.0f),
WINDOWS_8_1(6.3f),
WINDOWS_8(6.2f),
WINDOWS_7(6.1f),
WINDOWS_VISTA(6.0f),
WINDOWS_XP_64(5.2f),
WINDOWS_XP(5.1f),
WINDOWS_ME(4.9f),
WINDOWS_2000(5.0f),
WINDOWS_NT(4.0f), // or Win95
UNKNOWN(0.0f);
private float version;
WindowsType(float version) {
this.version = version;
}
public static float parseFloat(String versionString) {
float version = 0.0f;
/*
* Sanitize the String.
*
* Windows version is generally formatted x.x (e.g. 3.1, 6.1, 10.0)
* This format can later be treated as a float for comparison.
* Since we have no guarantee the String will be formatted properly, we'll sanitize it.
* For more complex comparisons, try a SemVer library, such as zafarkhaja/jsemver. <3
*/
List<String> parts = Arrays.asList(versionString.replaceAll("[^\\d.]", "").split("\\."));
// Fix .add(...), .remove(...), see https://stackoverflow.com/a/5755510/3196753
parts = new ArrayList<>(parts);
// Fix length
while (parts.size() != 2) {
if (parts.size() > 2) {
// pop off last element
parts.remove(parts.size() - 1);
}
if (parts.size() < 2) {
// push zero
parts.add("0");
}
}
String sanitized = String.join(".", parts.toArray(new String[0]));
try {
version = Float.parseFloat(sanitized);
} catch (NumberFormatException e) {
System.err.println("ERROR: Something went wrong parsing " + sanitized + " as a float");
}
return version;
}
public static WindowsType match(float version) {
WindowsType detectedType = UNKNOWN;
// Warning: Iterates in order they were declared. If you don't like this, write a proper comparator instead. <3
for (WindowsType type : WindowsType.values()) {
if (type.version >= version) {
detectedType = type;
} else {
break;
}
}
return detectedType;
}
}
public static void main(String... args) {
String osName = System.getProperty("os.name");
String osVer = System.getProperty("os.version");
if (osName.toLowerCase().startsWith("windows")) {
System.out.println("Yes, you appear to be running windows");
float windowsVersion = WindowsType.parseFloat(osVer);
System.out.println(" - Windows version reported is: " + windowsVersion);
WindowsType windowsType = WindowsType.match(windowsVersion);
System.out.println(" - Windows type is detected as: " + windowsType);
if(windowsVersion >= WindowsType.WINDOWS_XP.version) {
System.out.println("Yes, this OS is Windows XP or higher.");
} else {
System.out.println("No, this OS is NOT Windows XP or higher.");
}
}
}
}