Languages
[Edit]
EN

Java - detect operating system name (Windows, Linux, macOS)

4 points
Created by:
Payne
654

In this short article, we would like to show how to detect used operating system in Java application.

Quick solution:

String osName = System.getProperty("os.name");

// Windows 10
// Linux
// Mac OS X

// etc.

or:

// import org.apache.commons.lang3.SystemUtils;

boolean isWindows = SystemUtils.IS_OS_WINDOWS;
boolean isLinux = SystemUtils.IS_OS_LINUX;
boolean isMac = SystemUtils.IS_OS_MAC;

Where: SystemUtils should be attached as dependecy (Maven dependency is avaialble here).

 

Operating system name detection

In this section, you can find information what os.name property should return on specific operating system.

public class Program {

    public static void main(String[] args) {

        // e.g. when running on Windows 10
        System.out.println(System.getProperty("os.name"));  // Windows 10

        // e.g. when running on Ubuntu
        System.out.println(System.getProperty("os.name"));  // Linux

        // e.g. when running on MacBook
        System.out.println(System.getProperty("os.name"));  // Mac OS X
    }
}

 

Operating system type detection

Knowledge about operating system name may be requred when some operating system uses specific configuration or resources. In this section we would like to show different aproaches to detect operating system type.

 

1. Apache Commons library

Using this approach it is required to attach commons-lang3 library provided by Apache Software Foundation.

Usage example (it was run under Windows 10):

import org.apache.commons.lang3.SystemUtils;

public class Program {

    public static void main(String[] args) {

        // Most popular usage:
        System.out.println(SystemUtils.IS_OS_WINDOWS);  // true
        System.out.println(SystemUtils.IS_OS_LINUX);    // false
        System.out.println(SystemUtils.IS_OS_MAC);      // false

        // Less popular usage:
        System.out.println(SystemUtils.IS_OS_SOLARIS);  // false
        System.out.println(SystemUtils.IS_OS_SUN_OS);   // false
    }
}

Hint:

Using commons-lang3 library we are able to detect more precised OS version, e.g. SystemUtils.IS_OS_WINDOWS_8, SystemUtils.IS_OS_WINDOWS_10, etc.

Example output:

true
false
false
false
false

 

Maven dependency:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

Source: https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.12.0

 

2. Custom solution

In this section, you can find simple custom solution that lets to detect Windows, Linux or Mac operating system. For more precised detection it is recommended to use commons-lang3 library.

Usage example (it was run under Windows 10):

public class Program {
    
    public static void main(String[] args) {
        
        System.out.println("OS code: " + SystemUtils.OS_CODE);           // OS code: Windows
        System.out.println("OS name: " + SystemUtils.OS_NAME);           // OS name: Windows 10

        System.out.println("is Windows: " + SystemUtils.IS_OS_WINDOWS);  // is Windows: true
        System.out.println("is Linux: " + SystemUtils.IS_OS_LINUX);      // is Linux: true
        System.out.println("is Mac: " + SystemUtils.IS_OS_MAC);          // is Mac: true
    }
}

Example output:

OS code: Windows
OS name: Windows 10
is Windows: true
is Linux: false
is Mac: false

 

Example SystemUtils.java file:

public class SystemUtils {

    /** Returns OS name, e.g. Windows 10, Linux, Mac OS X, etc. */
    public static final String OS_NAME = getOSName();

    private static final String OS_TEXT = OS_NAME.toLowerCase();

    public static final boolean IS_OS_WINDOWS = OS_TEXT.startsWith("windows");
    public static final boolean IS_OS_LINUX = OS_TEXT.startsWith("linux");
    public static final boolean IS_OS_MAC = OS_TEXT.startsWith("mac");

    /** Returns OS code, e.g. Windows, Linux, Mac, Unknown */
    public static final String OS_CODE = getOSCode();

    private SystemUtils() {
        // Nothing here ...
    }

    private static String getOSName() {
        return System.getProperty("os.name");
    }

    private static String getOSCode() {
        if (IS_OS_WINDOWS) {
            return "Windows";
        }
        if (IS_OS_LINUX) {
            return "Linux";
        }
        if (IS_OS_MAC) {
            return "Mac";
        }
        return "Unknown";
    }
}

 

References

  1. System.getProperty - java docs
  2. https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.12.0

Alternative titles

  1. Java - check operating system name (Windows, Linux or Mac OS)
  2. Java - detect OS name
  3. Java - check OS name (Windows, Linux or Mac OS)
  4. Java - get operating system name
  5. Java - get operating system name (Windows, Linux or Mac OS)
  6. Java - get OS name
  7. Java - get OS name (Windows, Linux or Mac OS)
  8. Java - detect OS type
  9. Java - get OS type
  10. Java - check OS type
  11. Java - determine operating system
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join