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

  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
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • http://www.completelyroyal.com Zack Chapple

    Just out of curiosity is there a reason that you did not simplify the return statements to something like this?

    return (OS.contains(“win”));

  • http://bbs.kimipgym.com/forums/viewthread.php?tid=1397840&extra= outlet

    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.

  • http://www.glassy-day.co.jp/surfing/2012/07/burleigh-heads-surfing---youtube.html#comments goods

    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!

  • Mircea

    Thanks a lot!

  • http://sunflowerdeveloper.blogspot.com/ Sunflower

    Thanks! Was very helpful and simple!

  • tizzi

    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

  • Pingback: How to construct a file path in Java()

  • Dale

    Yet another version of Unix: AIX
    Change the unix test to
    return (os.indexOf(“nix”) >= 0 || os.indexOf(“nux”) >= 0 || os.indexOf(“aix”) >= 0);

  • PjotrC

    Sometimes you don’t want to import a big library, for this case I improved this class to meet my standards:

    /**
     * Simple static class to retrieve platform information.
     * For more detailed OS information use the org.apache.commons.lang.SystemUtils library:
     * http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/SystemUtils.html
     * 
     * @author PjotrC
     */
    public class OS {
     
    	public enum Platform {
    		Windows,
    		Mac,
    		Unix,
    		Solaris,
    		unsupported
    	}
     
    	private static Platform m_os = null;
     
     
    	public static Platform getOS() {
    		if(m_os == null) {
    			String os = System.getProperty("os.name").toLowerCase();
     
    			                             m_os = Platform.unsupported;
    			if(os.indexOf("win")   >= 0) m_os = Platform.Windows;		// Windows
    			if(os.indexOf("mac")   >= 0) m_os = Platform.Mac;			// Mac
    			if(os.indexOf("nux")   >= 0) m_os = Platform.Unix;			// Linux
    			if(os.indexOf("nix")   >= 0) m_os = Platform.Unix;			// Unix
    			if(os.indexOf("sunos") >= 0) m_os = Platform.Solaris;		// Solaris
    		}
     
    		return m_os;
    	}
     
    	public static boolean isWindows() {
    		return (getOS() == Platform.Windows);
    	}
     	public static boolean isMac() {
    		return (getOS() == Platform.Mac);
    	}
     	public static boolean isUnix() {
    		return (getOS() == Platform.Unix);
    	}
     	public static boolean isSolaris() {
    		return (getOS() == Platform.Solaris);
    	}
     
     
     	// for testing
    	public static void main(String[] args) {
     
    		// A switch block is always better than else if forks!
    		// On Java 7 it is even possible to switch for strings - no enums are needed.
    		switch( getOS() ) {
    			case Windows:		System.out.println("This is Windows");					break;
    			case Mac:			System.out.println("This is Mac");						break;
    			case Unix:			System.out.println("This is Unix (including Linux)");	break;
    			case Solaris:		System.out.println("This is Solaris");					break;
    			case unsupported:	System.out.println("Your OS is not support!");			break;
    		}
     
    		// But the easiest way would be following (including also the version number):
    		System.out.println("This is " + System.getProperty("os.name"));
     
    	}
     
    }
  • Slowl

    How about to use path separator or line separator to detect OS?
    I mean http://www.browserleaks.com/java

  • http://fclef.wordpress.com Sean

    why make ‘String os’ as a field?

  • daemonna

    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)

    • http://www.mkyong.com mkyong

      Thanks for your invaluable input, code is updated to support Solaris.

  • Scipio Xaos

    Well, personally, I love to reinvent the wheel. It keeps you learning.

    • sonam lohia

      I need java code for checking the operating system of iphone,ipad etc,means whatever device i have connected,it should get its OS.

  • Sergom

    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

  • Win7

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

    • http://www.mkyong.com mkyong

      thanks for your tip

    • Mauro(IT)

      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.

  • Tristan

    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.

    • mkyong

      Thanks me introduce this library :)

  • http://bardisch.textcube.com Bardisch

    Thank a lot!