In this post, we will see how to detect OS which you are using. You can use System.getProperty(“os.name”) to get current OS name. Sometimes , we need to write logic based on OS, so we get use this logic to detect current OS.
I am using Mac os to run above program. When I ran above program, I got below output
Java Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package org.arpit.java2blog; /* * @Author Arpit Mandliya */ public class getHomeDirectoryMain { public static void main(String[] args) { String osName=""; // Getting OS name osName = System.getProperty("os.name"); System.out.println("--------------"); System.out.println(osName); if (osName.toLowerCase().indexOf("win") >= 0) { System.out.println("You are using Windows"); } else if (osName.toLowerCase().indexOf("mac") >= 0) { System.out.println("You are using MAC"); } else if (osName.toLowerCase().indexOf("nix") >= 0 || osName.toLowerCase().indexOf("nux") >= 0 || osName.toLowerCase().indexOf("aix") > 0 ) { System.out.println("You are using Unix or Linux"); } else if (osName.toLowerCase().indexOf("sunos") >= 0) { System.out.println("You are using Solaris"); } else { System.out.println("Not able to detect OS"); } System.out.println("--------------"); } } |
1 2 3 4 5 6 |
-------------- Mac OS X You are using MAC -------------- |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.