用Java检查Windows XP或更高版本?

时间:2013-08-29 14:10:55

标签: java windows operating-system detect

检查运行我的Java应用程序的操作系统是Windows XP还是更高版本的最简单方法是什么?

编辑:我知道System.getProperty("os.name"),但我不知道检查Windows运行版本的最佳方法和/或最有效方法。理想情况下,我还希望将其作为未来证明,以便在发布其他版本的Windows时,我不需要更改代码。

2 个答案:

答案 0 :(得分:1)

mkyong给出了一个教程:http://www.mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/

它依赖于System.getProperty("os.name")

答案 1 :(得分:0)

由于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,因此更有可能有人正在寻找更新的OS比较...这是一种更全面的方法。

注意:在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.");
            }
        }
    }

}