Get Operating System Name and Version in Java



In this article, we will discuss how to get the operating system name and version in Java. To get the name and version of the operating system we will be using the System.getProperty() method.

Steps to get the operating system name and version

The following are the steps to get the operating system name and version ?

  • We will start by defining a class named Demo and inside that we will create the main method.
  • Inside that main method, we will use the os.name and os.version to get the name and version of the operating system.
  • And at the end, we will print it on the console.

Java program to get operating system name and version

Below is an example to get operating system name and version ?

Open Compiler
public class Demo { public static void main(String []args){ System.out.println("OS Name: " + System.getProperty("os.name")); System.out.println("OS Version: " + System.getProperty("os.version")); } }

Output

OS Name: Linux
OS Version: 3.10.0-862.9.1.el7.x86_64

Code Explanation

In the code, we create a simple Java class named Demo with a main method, which contains the entry point of the program.

And inside that we are using the System.getProperty() method in Java to get the operating system name and version.

Its syntax is ?

String getProperty(String key)

Above, the key is the name of the system property. Since, we want the operating system name and version, therefore we will add the key as ?

Operating System name - os.name
Operating System version - os.version

Finally, these values are printed to the console using System.out.println().

Updated on: 2024-08-27T18:48:33+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements