Main Tutorials

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

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
11 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
PEDRO RUBINI
6 years ago

Very nice!
Alternatively, I think you could just use:
return OS.contains(“…”);

MD Aftab Alam
7 years ago

Thank you very much sir your appreciate help 🙂

Zack Chapple
10 years ago

Just out of curiosity is there a reason that you did not simplify the return statements to something like this?

return (OS.contains(“win”));

GodZilL
5 years ago
Reply to  Zack Chapple

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;
}

daemonna
12 years ago

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)

Marco Ottina
3 years ago

Wonderful, works perfecly on Windows 10

Dale
11 years ago

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

Sean
12 years ago

why make ‘String os’ as a field?

Scipio Xaos
12 years ago

Well, personally, I love to reinvent the wheel. It keeps you learning.

sonam lohia
11 years ago
Reply to  Scipio Xaos

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