Detect OS Name and Version Java

Java is Platform independent and can run everywhere. Knowing this, it can be worth knowing in what operation system the application is running. To detect the current operation system, you can use the OSInfo class below, which retrieves the information from the system properties and returns an OS enum that holds the name and version of the operating system the application is running on.

Detect Operating System

We can get the current operation system name by calling System.getProperty("");. We evaluate this value and appropriately map it to the correct os. Finally we add the version number of the os by calling the System.getProperty("os.version");.

package com.memorynotfound.file;

import java.util.Locale;

public class OSInfo {

    public enum OS {

        private String version;

        public String getVersion() {
            return version;

        public void setVersion(String version) {
            this.version = version;

    private static OS os = OS.OTHER;

    static {
        try {
            String osName = System.getProperty("");
            if (osName == null) {
                throw new IOException(" not found");
            osName = osName.toLowerCase(Locale.ENGLISH);
            if (osName.contains("windows")) {
                os = OS.WINDOWS;
            } else if (osName.contains("linux")
                    || osName.contains("mpe/ix")
                    || osName.contains("freebsd")
                    || osName.contains("irix")
                    || osName.contains("digital unix")
                    || osName.contains("unix")) {
                os = OS.UNIX;
            } else if (osName.contains("mac os")) {
                os = OS.MAC;
            } else if (osName.contains("sun os")
                    || osName.contains("sunos")
                    || osName.contains("solaris")) {
                os = OS.POSIX_UNIX;
            } else if (osName.contains("hp-ux") 
                    || osName.contains("aix")) {
                os = OS.POSIX_UNIX;
            } else {
                os = OS.OTHER;

        } catch (Exception ex) {
            os = OS.OTHER;
        } finally {

    public static OS getOs() {
        return os;

Detecting the current os the application is running on.

package com.memorynotfound.file;

public class DetectOS {

    public static void main(String... args){
        System.out.println("OS: " + OSInfo.getOs());
        System.out.println("OS version: " + OSInfo.getOs().getVersion());
        System.out.println("Is mac? " + OSInfo.OS.MAC.equals(OSInfo.getOs()));


OS version: 10.10.5
Is mac? true


You may also like...