Java Program to Detect the Name and Version of the Operating System
What is an Operating System ?
An operating system (OS) is a software that manages and controls the hardware and software resources of a computer and provides common services for computer programs. It acts as an interface between the user, hardware, and software applications. Examples of operating systems include Windows, macOS, Linux, and Android.
Ways to detect the name and version of an OS in Java :
- System Properties: You can use the System.getProperty(“os.name”) method to get the name of the operating system and System.getProperty(“os.version”) method to get the version of the operating system.
- System Environment Variables: You can use the System.getenv(“OS”) method to get the name of the operating system and System.getenv(“OS_VERSION”) method to get the version of the operating system.
- Java Management Extensions (JMX): You can use the OperatingSystemMXBean interface to get the name and version of the operating system.
- Using Apache Commons Lang: You can use the SystemUtils class from Apache Commons Lang library to get the name and version of the operating system.
Example : Java code for getting the operating system name and version using System Properties:
public class Main { public static void main(String[] args) { String osName = System.getProperty("os.name"); String osVersion = System.getProperty("os.version"); // Output the operating system name and version System.out.println("Operating System Name: " + osName); System.out.println("Operating System Version: " + osVersion); } }
Output :
Operating System Name: Linux Operating System Version: 5.4.0-1093-gcpNote : OS name and version may be differ for different system.
Explanation :
In this example we uses the System.getProperty(“os.name”) method to get the name of the operating system and System.getProperty(“os.version”) method to get the version of the operating system.
The System.getProperty(String key) method returns the value of the specified system property. The os.name and os.version keys are predefined system properties that provide information about the operating system.
The code then outputs the operating system name and version using the System.out.println() method.
Example : Java code for getting the operating system name and version using System Environment Variables:
public class Main { public static void main(String[] args) { String osName = System.getenv("OS"); String osVersion = System.getenv("OS_VERSION"); // Check if the environment variables are not null if (osName != null && osVersion != null) { // Output the operating system name and version System.out.println("Operating System Name: " + osName); System.out.println("Operating System Version: " + osVersion); } else { System.out.println("OS name or version not found in environment variables."); } } }
Output :
OS name or version not found in environment variables.
Explanation :
In this example we uses the System.getenv(“OS”) method to get the name of the operating system and System.getenv(“OS_VERSION”) method to get the version of the operating system.
The System.getenv(String name) method returns the value of the specified environment variable. The OS and OS_VERSION environment variables are specific to the operating system and provide information about the operating system.
The code then checks if the environment variables are not null using an if statement. If they are not null, the code outputs the operating system name and version using the System.out.println() method. If they are null, the code outputs a message indicating that the OS name or version was not found in the environment variables.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment