Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ā¤ šŸ’»

If you think sharing knowledge and helping other is valuable.

join our community - Click here

Java - How to check OS name and if I run application on Windows, Linux or Mac OS?

0 contributions
1 points

In java we can getĀ operating system name by using:

System.getProperty("os.name")

1.Ā Java get operating system name

Code example:

public class JavaGetOperatingSystemName {

    public static void main(String[] args) {

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

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

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

2. Java checkĀ operating system name with apache commons lib

Code example:

import org.apache.commons.lang3.SystemUtils;

public class SystemUtilsApacheCommonsExample {

    public static void main(String[] args) {

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

        // less popular:
        System.out.println(SystemUtils.IS_OS_SOLARIS);
        System.out.println(SystemUtils.IS_OS_SUN_OS);
    }
}

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.8.1</version>
</dependency>

3. Java checkĀ operating system name with custom SystemUtil

Code example:

public class SystemUtilsCustom {

    public static void main(String[] args) {

        System.out.println(OS); // windows 10

        if (isWindows()) {
            System.out.println("We are on Windows"); // We are on Windows
        } else if (isMac()) {
            System.out.println("We are on Mac");
        } else if (isUnix()) {
            System.out.println("We are on Unix or Linux");
        } else if (isSolaris()) {
            System.out.println("We are on Solaris");
        } else {
            System.out.println("Our OS is not support yet");
        }

        System.out.println(getCurrentOS()); // Windows
    }

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

    public static boolean isWindows() {
        return OS.contains("win");
    }

    public static boolean isMac() {
        return OS.contains("mac");
    }

    public static boolean isUnix() {
        return (OS.contains("nix") || OS.contains("nux") || OS.contains("aix"));
    }

    public static boolean isSolaris() {
        return OS.contains("sunos");
    }

    public static String getCurrentOS() {
        if (isWindows()) {
            return "Windows";
        } else if (isMac()) {
            return "Mac OS X";
        } else if (isUnix()) {
            return "Unix";
        } else if (isSolaris()) {
            return "Solaris";
        } else {
            return "This OS is not support";
        }
    }
}

Output:

windows 10
We are on Windows
Windows

References

  1. System.getProperty - java docs
0 contributions

Suggested wiki articles:

Suggested findings

Let's do the work and have some fun
  • Dirask is online IT community for professionals and hobbyist to share their knowledge and help each other in extraordinary easy way. ā¤ šŸ’» šŸ™‚
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    we will help you. šŸ‘ āœ” šŸ¦„
Read more