Open In App

Java Program to Determine the Name and Version of the Operating System

Last Updated : 22 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The inbuilt System class in Java provides getProperty() method which is used to obtain the properties of the current working operating system. The System class has two versions of getProperty(). Both retrieve the value of the property named in the argument list.

Methods:

  1. getProperty() version 1
  2. getProperty() version 2

Version 1: One of the versions of getProperty() method takes a single argument as property and returns a string containing the value of the property. If the property does not exist, this version of getProperty returns null.

Syntax:

String System.getProperty( String key ) ;

 Parameter: Key only, which is the property of the operating system.

 Return Type:

  • Returns a string containing the value of the property.
  • Returns Null if the property does not exist.

Version 2: The other version of getProperty() takes two String arguments, the first argument as property and the second argument is a default value to return if the key cannot be found or if it has no value.

Syntax:

String System.getProperty( String key, String value ) ;

 Parameter:

  • Key is the property of the operating system.
  • The default value of the key to be specified in case of invalid property.

 Return Type:

  • Returns a string containing the value of the property.
  • Returns the default value provided as the second argument in case of an invalid system property.

Example: To figure out the name and version of the operating system.

Java




// Java Program to Determine the name
// and version of the operating system
  
// Importing all classes of
// java.util package
import java.util.*;
  
public class GFG {
  
    // Getting name of the OS
    private static final String nameOfOs
        = System.getProperty("os.name");
  
    // Getting version of the OS
    private static final String versionOfOS
        = System.getProperty("sun.arch.data.model");
  
    // Main driver method
    public static void main(String[] args)
    {
        // Printing name of OS
        System.out.println(
            "Name of the operating system is " + nameOfOs);
  
        // Printing version of the OS
        System.out.println(
            "Version of the operating system is "
            + versionOfOS);
    }
}


Output: The above program is compiled and run on terminal and the output is as follows:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads