How to detect OS in Java – System.getProperty(“os.name”)
Published: May 4, 2009 , Updated: December 2, 2011 , Author: mkyong
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”.
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 if (isSolaris()) { System.out.println("This is Solaris"); } 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); } public static boolean isSolaris() { String os = System.getProperty("os.name").toLowerCase(); // Solaris 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
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
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.
Dynamically getting and parsing of os.name is not a good idea. Use static
private static String os = System.getProperty(“os.name”).toLowerCase();
private static boolean isUnix = os.indexOf(“nix”) >= 0 || os.indexOf(“nux”) >= 0;
private static boolean isWindows = os.indexOf(“win”) >= 0;
private static boolean isMac = os.indexOf(“mac”) >= 0;
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!