Aide mémoire : Détection de l’OS

Aide mémoire (Développement) / Uncategorized
Aide mémoire pour détecter l’OS sur lequel votre programme java tourne…

/**
 * Use System.getProperty("os.name") to detect which type of operating  system (OS) you are using now.
 * This code can detect Windows, Mac, Unix and Solaris. 
 * @see http://www.mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/
 * @author http://www.mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/
 */
public final class OsValidator {

    /**
     * Operating System (OS) you are using now.
     */
    public static String OS = System.getProperty("os.name").toLowerCase();

    private OsValidator() {
    }

    /**
     * @return true if current os is Windows based.
     */
    public static boolean isWindows() {
        return (OS.indexOf("win") >= 0);
    }

    /**
     * @return true if current os is Mac based
     */
    public static boolean isMac() {
        return (OS.indexOf("mac") >= 0);
    }

    /**
     * @return true if current os is Unix based
     */
    public static boolean isUnix() {
        return (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0);
    }

    /**
     * @return true if current os is Solaris based
     */
    public static boolean isSolaris() {
        return (OS.indexOf("sunos") >= 0);
    }
}