Question

What would be the easiest way to check that the operating system running my Java application is Windows XP or later?

EDIT: I know about System.getProperty("os.name") but I don't know the best way and/or most efficient way to check what version of Windows is running. Ideally, I would also like to make it future proof so that if another version of Windows is released I don't need to change the code.

Was it helpful?

Solution

mkyong has given a tutorial: http://www.mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/

It relies on System.getProperty("os.name")

OTHER TIPS

Since Windows reliably reports its version information as a float, "XP or higher" can be calculated by knowing what each Windows version is and comparing.

The shorthand:

// 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);

But due to exceptions that can be raised with parsing a System value, combined with the fact that XP is EOL and it's more likely that someone is looking for a newer OS comparison... here's a more comprehensive way to do it.

Note: With Windows 10, all versions are reported as 10.0 regardless of how much Microsoft changed between releases. For finer detail, you will have to look into the registry (See also 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.");
            }
        }
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top