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:
- getProperty() version 1
- 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
import java.util.*;
public class GFG {
private static final String nameOfOs
= System.getProperty( "os.name" );
private static final String versionOfOS
= System.getProperty( "sun.arch.data.model" );
public static void main(String[] args)
{
System.out.println(
"Name of the operating system is " + nameOfOs);
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
Share your thoughts in the comments
Please Login to comment...