Skip to content
Related Articles

Related Articles

Java Program to Determine the Name and Version of the Operating System
  • Last Updated : 22 Dec, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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


Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :