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
This article was posted in Java category.
Oracle Magazine - Free Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for Java's developers and DBAs, and more.
Securing & Optimizing Linux: The Hacking Solution - Free Guide
A comprehensive collection of Linux security products and explanations in the most simple and structured manner on how to safely and easily configure and run many popular Linux-based applications and services.
The Windows 7 Guide: From Newbies to Pros - Free Guide
In this 46 page guide you will be introduced to Windows 7 and what it has to offer. This guide will go over the software compatible issues, you will learn about the new taskbar, how to use and customize Windows Aero, what Windows 7 Libraries are all about, what software is included in Windows 7, and how easy networking is with Windows 7 along with other topics.
All Java Tutorials
- Java Core Technology - Java RegEx, Java XML, Java I/O, Java Misc
- J2EE Frameworks - Hibernate, Spring 2.5, Spring MVC, Struts 1.x, Struts 2.x
- Build Tools - Maven, Archiva
- Unit Test - jUnit, TestNG
- Client Scripts - jQuery
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!