How do I programmatically determine operating system in Java?

后端 未结 19 2914
眼角桃花
眼角桃花 2020-11-22 04:56

I would like to determine the operating system of the host that my Java program is running programmatically (for example: I would like to be able to load different propertie

19条回答
  •  执笔经年
    2020-11-22 05:15

    I liked Wolfgang's answer, just because I believe things like that should be consts...

    so I've rephrased it a bit for myself, and thought to share it :)

    /**
     * types of Operating Systems
     *
     * please keep the note below as a pseudo-license
     *
     * helper class to check the operating system this Java VM runs in
     * http://stackoverflow.com/questions/228477/how-do-i-programmatically-determine-operating-system-in-java
     * compare to http://svn.terracotta.org/svn/tc/dso/tags/2.6.4/code/base/common/src/com/tc/util/runtime/Os.java
     * http://www.docjar.com/html/api/org/apache/commons/lang/SystemUtils.java.html
     */
    public enum OSType {
        MacOS("mac", "darwin"),
        Windows("win"),
        Linux("nux"),
        Other("generic");
    
        private static OSType detectedOS;
    
        private final String[] keys;
    
        private OSType(String... keys) {
            this.keys = keys;
        }
    
        private boolean match(String osKey) {
            for (int i = 0; i < keys.length; i++) {
                if (osKey.indexOf(keys[i]) != -1)
                    return true;
            }
            return false;
        }
    
        public static OSType getOS_Type() {
            if (detectedOS == null)
                detectedOS = getOperatingSystemType(System.getProperty("os.name", Other.keys[0]).toLowerCase());
            return detectedOS;
        }
    
        private static OSType getOperatingSystemType(String osKey) {
            for (OSType osType : values()) {
                if (osType.match(osKey))
                    return osType;
            }
            return Other;
        }
    }
    

提交回复
热议问题