Determine Operating System and its version in Java
This post will discuss how to determine the operating system version programmatically from Java using Java System Properties, Apache Commons Lang, and JavaFX classes.
1. Using System Properties
Java System class provides access to externally defined properties and environment variables. The simplest and widely used solution is to use the os.name system property to determine the operating system using the System.getProperty() method. This is shown below:
|
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
class Main { // create enums for each operating system public enum OS { WINDOWS, LINUX, MAC, SOLARIS }; public static OS getOperatingSystem() { // detecting the operating system using `os.name` System property String os = System.getProperty("os.name").toLowerCase(); if (os.contains("win")) { return OS.WINDOWS; } else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) { return OS.LINUX; } else if (os.contains("mac")) { return OS.MAC; } else if (os.contains("sunos")) { return OS.SOLARIS; } return null; } public static void main(String[] args) { switch (getOperatingSystem()) { case WINDOWS: System.out.println("Windows Operating System"); break; case LINUX: System.out.println("Linux Operating System"); break; case MAC: System.out.println("Mac Operating System"); break; case SOLARIS: System.out.println("Solaris Operating System"); break; default: System.out.println("Unknown Operating System"); } } } |
2. Using Apache Commons Lang
Apache Commons Lang SystemUtils class provides several helpers for java.lang.System properties. For example, SystemUtils.OS_NAME returns the os.name system Property.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import org.apache.commons.lang3.SystemUtils; class Main { public static String getOperatingSystem() { return SystemUtils.OS_NAME; } public static void main(String[] args) { System.out.println(getOperatingSystem() + " Operating System"); } } |
The SystemUtils class also offers several static fields that decide the operating system, such as IS_OS_WINDOWS, IS_OS_LINUX, IS_OS_UNIX, IS_OS_MAC, IS_OS_SOLARIS, etc., as shown below:
|
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 |
import org.apache.commons.lang3.SystemUtils; class Main { public static String getOperatingSystem() { return SystemUtils.OS_NAME; } public static void main(String[] args) { if (SystemUtils.IS_OS_WINDOWS) { System.out.println("Windows Operating System"); } else if (SystemUtils.IS_OS_LINUX) { System.out.println("Linux Operating System"); } else if (SystemUtils.IS_OS_UNIX) { System.out.println("Unix Operating System"); } else if (SystemUtils.IS_OS_MAC) { System.out.println("Mac Operating System"); } else if (SystemUtils.IS_OS_SOLARIS) { System.out.println("Solaris Operating System"); } else { System.out.println(getOperatingSystem() + " Operating System"); } } } |
3. Using JavaFX classes
We can also use the static methods offered by the following JavaFX classes to detect the operating system.
com.sun.media.jfxmediaimpl.HostUtils
com.sun.javafx.util.Utils`
Here’s a working example:
|
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 |
import com.sun.javafx.PlatformUtil; class Main { public static void main(String[] args) { if (PlatformUtil.isWindows()) { System.out.println("Windows Operating System"); } else if (PlatformUtil.isLinux()) { System.out.println("Linux Operating System"); } else if (PlatformUtil.isUnix()) { System.out.println("Unix Operating System"); } else if (PlatformUtil.isMac()) { System.out.println("Mac Operating System"); } else if (PlatformUtil.isSolaris()) { System.out.println("Solaris Operating System"); } else if (PlatformUtil.isAndroid()) { System.out.println("Android Operating System"); } else if (PlatformUtil.isIOS()) { System.out.println("IOS Operating System"); } } } |
That’s all about determining the Operating System and its version in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)