There are several System properties which can be accessed using System.getProperty("property_name")
where os.name
is one of those properties.
The below code help us identify the operating system of Java Virtual Machine (JVM) using System.getProperty("os.name")
.
package com.learning;
import java.util.Locale;
public class OperatingSystem {
private static final String OS = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
public static final String WINDOWS_OS = "windows";
public static final String MAC_OS = "mac";
public static final String UNIX_LINUX_OS = "unixLinux";
public static final String SOLARIS_OS = "solaris";
public static boolean isWindows() {
return OS.contains("win");
}
public static boolean isMac() {
return OS.contains("mac");
}
public static boolean isUnix() {
return (OS.contains("nix") || OS.contains("nux") || OS.contains("aix"));
}
public static boolean isSolaris() {
return OS.contains("sunos");
}
public static void main(String args[]) {
getOperatingSystemType();
}
public static String getOperatingSystemType() {
if (isWindows()) {
System.out.println("************** Windows OS Detected ***************");
return WINDOWS_OS;
} else if (isMac()) {
System.out.println("************** Mac OS Detected ***************");
return MAC_OS;
} else if (isUnix()) {
System.out.println("************** Unix or Linux OS Detected ***************");
return UNIX_LINUX_OS;
} else if (isSolaris()) {
System.out.println("************** Solaris OS Detected ***************");
return SOLARIS_OS;
}
return null;
}
}
After executing the above code on Windows Operating System Machine the below output is generated.
************** Windows OS Detected ***************