How to detect OS in Java

detect os

This article shows a handy Java class that uses System.getProperty("os.name") to detect which type of operating system (OS) you are using now.

1. Detect OS (Original Version)

This code can detect Windows, Mac, Unix, and Solaris.

OSValidator.java

package com.mkyong.system;

public class OSValidator {

    private static String OS = System.getProperty("os.name").toLowerCase();

    public static void main(String[] args) {

        System.out.println("os.name: " + OS);

        if (isWindows()) {
            System.out.println("This is Windows");
        } else if (isMac()) {
            System.out.println("This is Mac");
        } else if (isUnix()) {
            System.out.println("This is Unix or Linux");
        } else if (isSolaris()) {
            System.out.println("This is Solaris");
        } else {
            System.out.println("Your OS is not support!!");
        }
    }

    public static boolean isWindows() {
        return (OS.indexOf("win") >= 0);
    }

    public static boolean isMac() {
        return (OS.indexOf("mac") >= 0);
    }

    public static boolean isUnix() {
        return (OS.indexOf("nix") >= 0
                || OS.indexOf("nux") >= 0
                || OS.indexOf("aix") > 0);
    }

    public static boolean isSolaris() {
        return (OS.indexOf("sunos") >= 0);
    }

}

Output, run on Windows 10.

Terminal

os.name: windows 10
This is Windows

2. Detect OS (Enhanced Version)

Since the operating system will remain the same for the running Java app, we can increase the performance by moving the OS checking to static fields; The static ensures the OS.indexOf checking runs once only.

OSValidator.java

package com.mkyong.system;

public class OSValidator {

    private static String OS = System.getProperty("os.name").toLowerCase();
    public static boolean IS_WINDOWS = (OS.indexOf("win") >= 0);
    public static boolean IS_MAC = (OS.indexOf("mac") >= 0);
    public static boolean IS_UNIX = (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0);
    public static boolean IS_SOLARIS = (OS.indexOf("sunos") >= 0);

    public static void main(String[] args) {

        System.out.println("os.name: " + OS);

        if (IS_WINDOWS) {
            System.out.println("This is Windows");
        } else if (IS_MAC) {
            System.out.println("This is Mac");
        } else if (IS_UNIX) {
            System.out.println("This is Unix or Linux");
        } else if (IS_SOLARIS) {
            System.out.println("This is Solaris");
        } else {
            System.out.println("Your OS is not support!!");
        }
    }

}

Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-basic/system

References

11 comments on “How to detect OS in Java

    1. Take a look at the java.lang.String source code.
      Basically both are the same:

      public boolean contains(CharSequence s) {
      return indexOf(s.toString()) > -1;
      }

  1. Yet another version of Unix: AIX
    Change the unix test to
    return (os.indexOf(“nix”) >= 0 || os.indexOf(“nux”) >= 0 || os.indexOf(“aix”) >= 0);

  2. unix keyword doesnt work on Solaris, so fix is

    System.getProperty(“os.name”).toLowerCase().indexOf(“sunos”)

    for SOLARIS 10 and higher (didnt try older), as Solaris is still pretty used in enterprise and is main JAVA platform (regarding performance)

    1. I need java code for checking the operating system of iphone,ipad etc,means whatever device i have connected,it should get its OS.

Leave a Comment

Your email address will not be published. Required fields are marked *