Java – Determine if the running OS is Windows, Mac or Linux

The Apache Commons Lang library could help you to determine the running OS type. Here is an simple example.

pom.xml

...
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.1</version>
</dependency>
...

 

So now you could determine the OS by

...
import org.apache.commons.lang3.SystemUtils;
...

if (SystemUtils.IS_OS_WINDOWS) {
  System.out.println("This is a WINDOWS OS.");
} else if (SystemUtils.IS_OS_MAC) {
  System.out.println("This is a MAC OS.");
} else if (SystemUtils.IS_OS_LINUX) {
  System.out.println("This is a LINUX OS.");
} else {
  System.out.println("This is a SPECIAL OS.");
}
...

 

Done =)

Reference:

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.