Exercise:
Write a Java Program to determine the name and version of the operating system.
Click Here to View the Solution!
public class OSname {
public static void main(String[] args) {
// get the name of operating system
String operatingSystem = System.getProperty("os.name");
System.out.println(operatingSystem);
}
}
Click Here to View the Output!
Linux
Click Here to View the Explanation!
- This program is used to find out and print the name and version of the operating system that Java uses to execute its programs.
- A method
getProperty()
of the classSystem
in Java takes the keyos.name
as its parameter and stores the value in a string variableOperatingSystem
. - The method
getProperty()
simply returns that property of the system which is specified by the key in its parameter. - In this case of the key os.name, the method returns the name of the operating system i.e. Windows 10.
- The version of the operating system can also be determined by simply changing the key inside the
getProperty()
method toos.version
as follows:System.getProperty(“os.version”)
.