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

About the Author

author image
mkyong
Founder of Mkyong.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

Leave a Reply

avatar
newest oldest most voted
Zack Chapple
Guest
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”));

daemonna
Guest
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)

Tristan
Guest
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.

outlet
Guest
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.

tizzi
Guest
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

Dale
Guest
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
Guest
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") >=… Read more »
Slowl
Guest
Slowl

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

Sean
Guest
Sean

why make ‘String os’ as a field?

Scipio Xaos
Guest
Scipio Xaos

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

sonam lohia
Guest
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
Guest
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
Guest
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 )) ;

Mauro(IT)
Guest
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.

Bardisch
Guest
Bardisch

Thank a lot!

PEDRO RUBINI
Guest
PEDRO RUBINI

Very nice!
Alternatively, I think you could just use:
return OS.contains(“…”);

Vijayarajah Sinthuja
Guest
Vijayarajah Sinthuja

Much simpler code using SystemUtils from apache commons lang3

package com.sinthu.os;
import org.apache.commons.lang3.SystemUtils;
public class OSValidateCommons {

public static void main(String args[]){

System.out.println(“Name of the OS is: “+ SystemUtils.OS_NAME);

}
}

MD Aftab Alam
Guest
MD Aftab Alam

Thank you very much sir your appreciate help :)

goods
Guest
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… Read more »
Mircea
Guest
Mircea

Thanks a lot!

Sunflower
Guest
Sunflower

Thanks! Was very helpful and simple!

trackback
How to construct a file path in Java

[…] http://www.mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/ Tags : filepath io java mkyong Founder and Chief Editor of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. […]