How to detect OS in Java – System.getProperty(“os.name”)
Written on
May 4, 2009 at 10:24 am by
mkyong
Here is a handy Java class that use System.getProperty(“os.name”) to detect which type of operating system (OS) we are using now.
public class OSValidator{ public static void main(String[] args) { 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{ System.out.println("Your OS is not support!!"); } } public static boolean isWindows(){ String os = System.getProperty("os.name").toLowerCase(); //windows return (os.indexOf( "win" ) >= 0); } public static boolean isMac(){ String os = System.getProperty("os.name").toLowerCase(); //Mac return (os.indexOf( "mac" ) >= 0); } public static boolean isUnix(){ String os = System.getProperty("os.name").toLowerCase(); //linux or unix return (os.indexOf( "nix") >=0 || os.indexOf( "nux") >=0); } }
Reference
1) os.name value – http://lopica.sourceforge.net/os.html
2) System.getProperty() – http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html


Why don’t you just use Apache Commons, see: http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/SystemUtils.html
It has many useful classes you don’t have to write yourself.
Thanks me introduce this library
Thank a lot!