How to detect OS in Java
Here is a handy Java class that 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”.
package com.mkyong.core; public class OSValidator { private static String OS = System.getProperty("os.name").toLowerCase(); public static void main(String[] args) { System.out.println(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); } }
References
- os.name value – http://lopica.sourceforge.net/os.html
- System.getProperty() – http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html
Tags : java

The following time I learn a weblog, I hope that it doesnt disappoint me as much as this one. I imply, I do know it was my option to learn, but I truly thought youd have something interesting to say. All I hear is a bunch of whining about something that you may repair in case you werent too busy on the lookout for attention.
My husband and i were absolutely fortunate when Chris managed to deal with his studies via the precious recommendations he came across from your very own web pages. It’s not at all simplistic to just possibly be freely giving instructions which others could have been making money from. We fully understand we now have the blog owner to thank for this. All of the explanations you have made, the straightforward web site navigation, the friendships you can help engender – it’s many excellent, and it’s leading our son and our family reason why this idea is thrilling, which is certainly particularly serious. Many thanks for all the pieces!
Thanks a lot!
Thanks! Was very helpful and simple!
I used windows7. Could that be the reason why the code isn’t working for me? Please write back if there is any necessary modification to help the code work on my OS. thanks
Yet another version of Unix: AIX
Change the unix test to
return (os.indexOf(“nix”) >= 0 || os.indexOf(“nux”) >= 0 || os.indexOf(“aix”) >= 0);
Sometimes you don’t want to import a big library, for this case I improved this class to meet my standards:
How about to use path separator or line separator to detect OS?
I mean http://www.browserleaks.com/java
why make ‘String os’ as a field?
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)
Thanks for your invaluable input, code is updated to support Solaris.
Well, personally, I love to reinvent the wheel. It keeps you learning.
I need java code for checking the operating system of iphone,ipad etc,means whatever device i have connected,it should get its OS.
Dynamically getting and parsing of os.name is not a good idea. Use static
and simply static getters for all boolean fiels
with windows seven and java 1.6(with my test)
you have to change this:
return (os.indexOf( “win” ) >= 0);
into this:
return ((os.indexOf( “win” ) >= 0 ) || (os.indexOf( “Win” ) >= 0 )) ;
thanks for your tip
4 Win7
The code you added:
return (os.indexOf( “win” ) >= 0);
into this:
return ((os.indexOf( “win” ) >= 0 ) || (os.indexOf( “Win” ) >= 0 )) ;
sounds a bit strange, cause
the line
os = System.getProperty(“os.name”).toLowerCase()
should permit to ignore the letters case.
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!