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:

Download  Run Code

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.

Download Code

 
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:

Download Code

3. Using JavaFX classes

We can also use the static methods offered by the following JavaFX classes to detect the operating system.

com.sun.javafx.PlatformUtil
com.sun.media.jfxmediaimpl.HostUtils
com.sun.javafx.util.Utils`

Here’s a working example:

Download Code

That’s all about determining the Operating System and its version in Java.