Languages
[Edit]
DE

Java - Wie überprüft man den OS Namen und ob man eine Anwendung unter Windows, Linux oder Mac OS ausführen kann?

3 points
Created by:
Nikki
1620

In Java kann man den Namen des Betriebssystem erhalten, indem das Folgende verwendet wird:

System.getProperty("os.name")

1. Java Betriebssystemnamen abrufen

Codebeispiel:

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 Betriebssystemnamen überprüfen mit apache commons lib

Codebeispiel:

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

Ausgabe:

true
false
false
false
false

Maven-Abh├Ąngigkeit:

<!-- 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 Betriebssystemnamen überprüfen mit custom SystemUtil

Codebeispiel:

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

Ausgabe:

windows 10
We are on Windows
Windows

Literaturverzeichnis

  1. System.getProperty - java docs
Hey ­čĹő
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists 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,
    this community will help you.