Write a Java Program to Determine the name and version of the operating system

As a Java programmer, you may need to determine the name and version of the operating system on which your Java application is running.

This information is important for several reasons, such as identifying platform-specific bugs, optimizing performance, and determining compatibility with other software components.


Fortunately, Java provides a simple way to obtain this information using the System class, which provides access to various system properties.

One such property is “os.name”, which returns the name of the operating system.

Another property is “os.version”, which returns the version number of the operating system.

To demonstrate how to use these properties, let’s write a simple Java program that prints the name and version of the operating system:

public class OSInfo {
    public static void main(String[] args) {
        String osName = System.getProperty("os.name");
        String osVersion = System.getProperty("os.version");
        System.out.println("Operating System Name: " + osName);
        System.out.println("Operating System Version: " + osVersion);
    }
}

In this program, we first obtain the value of “os.name” and “os.version” using the System.getProperty() method.

We then print these values to the console using the System.out.println() method.

When you run this program on different operating systems, you will get different outputs.

For example, if you run this program on a Windows 10 machine, you will see something like this:

Operating System Name: Windows 10
Operating System Version: 10.0

On the other hand, if you run this program on a macOS Big Sur machine, you will see something like this:

Operating System Name: Mac OS X
Operating System Version: 11.0.1

As you can see, the program correctly identifies the name and version of the operating system in each case.


In conclusion, determining the name and version of the operating system on which your Java application is running is a straightforward task that can be accomplished using the System class in Java.

By using this information, you can ensure that your Java application is running optimally on different platforms and that it is compatible with other software components.