How to get os version in java

Java Program to get Operating System name and version

Use the System.getProperty() method in Java to get the Operating System name and version.

String getProperty(String key)

Above, the key is the name of the system property. Since, we want the OS name and version, therefore we will add the key as −

Operating System name - os.name Operating System version - os.version

The following is an example −

Example

Output

OS Name: Linux OS Version: 3.10.0-862.9.1.el7.x86_64

Samual Sam

Learning faster. Every day.

  • Related Articles
  • Java Program to determine the name and version of the operating system
  • Haskell Program to determine the name and version of the operating system
  • Display the Operating System name in Java
  • Get Operating System temporary directory / folder in Java
  • How to get android application version name?
  • Difference Between Network Operating System and Distributed Operating System
  • Java Program to get full day name
  • Java program to Get IP address of the system
  • Java Program to get name of parent directory
  • Difference between System Software and Operating System
  • How to get Java and OS version, vendor details in JShell in Java 9?
  • Operating System Design and Implementation
  • Difference between a Centralised Version Control System (CVCS) and a Distributed Version Control System (DVCS)
  • How to get the application name and version information of a browser in JavaScript?
  • Operating System Structure
Читайте также:  Python request username password

Источник

Get Operating System Information in Java using OSHI library

In this tutorial, we show you how to use OSHI library in a Java program to get operating system information. By different example Java programs we learn how to get information of operating system name, version, manufacturer, etc.

What is OSHI?

OSHI is a free JNA-based (native) Operating System and Hardware Information library for Java. It does not require the installation of any additional native libraries and aims to provide a cross-platform implementation to retrieve system information, such as OS version, processes, memory and CPU usage, disks and partitions, devices, sensors, etc.

For more information about the OSHI library you can visit the library repository at github.com/oshi/oshi

Add OSHI library to the Java project

To use OSHI Java library in the Gradle build project, add the following dependency into the build.gradle file.

compile group: 'com.github.oshi', name: 'oshi-core', version: '5.3.4'

To use OSHI Java library in the Maven build project, add the following dependency into the pom.xml file.

  com.github.oshi oshi-core 5.3.4 

How to use OSHI to get Operating System Information

To use the OSHI library we need to instantiate a SystemInfo object as this is the main entry point to the OSHI library.

SystemInfo systemInfo = new SystemInfo();

Then get the object of OperatingSystem to retrieve operating system information that your Java program is running on.

OperatingSystem operatingSystem = systemInfo.getOperatingSystem();

Example 1 Get Operating System Information

import oshi.SystemInfo; import oshi.software.os.OperatingSystem; public class GetOsInfoExample1  public static void main(String. args)  SystemInfo systemInfo = new SystemInfo(); OperatingSystem operatingSystem = systemInfo.getOperatingSystem(); System.out.println("Operating System: " + operatingSystem.toString()); > >
Operating System: Microsoft Windows 10.0 (Home) build 19042

Example 2 Get Details of Operating System

import oshi.SystemInfo; import oshi.software.os.OperatingSystem; public class GetOsInfoExample2  public static void main(String. args)  SystemInfo systemInfo = new SystemInfo(); OperatingSystem operatingSystem = systemInfo.getOperatingSystem(); System.out.println("Family: " + operatingSystem.getFamily()); System.out.println("Manufacturer: " + operatingSystem.getManufacturer()); System.out.println("Number of bits supported by the OS (32 or 64): " + operatingSystem.getBitness()); > >
Family: Windows Manufacturer: Microsoft Number of bits supported by the OS (32 or 64): 64

Example 3 Get Details of Operating System Version

import oshi.SystemInfo; import oshi.software.os.OperatingSystem; public class GetOsInfoExample3  public static void main(String. args)  SystemInfo systemInfo = new SystemInfo(); OperatingSystem operatingSystem = systemInfo.getOperatingSystem(); OperatingSystem.OSVersionInfo versionInfo = operatingSystem.getVersionInfo(); System.out.println("Version: " + versionInfo.getVersion()); System.out.println("CodeName: " + versionInfo.getCodeName()); System.out.println("Build Number: " + versionInfo.getBuildNumber()); > >
Version: 10.0 CodeName: Home Build Number: 19042

Источник

Get OS version, name, architecture, and system properties in Java

Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.

In Java, we can use the os.name system property to find the OS name and version. We can also use the os.version system property to get the version.

To access the system properties, we can call System.getProperty with the property name that we need to access.

class GetOSDetails
public static void main( String args[] )
//Operating system name
System.out.println("Your OS name -> " + System.getProperty("os.name"));
//Operating system version
System.out.println("Your OS version -> " + System.getProperty("os.version"));
//Operating system architecture
System.out.println("Your OS Architecture -> " + System.getProperty("os.arch"));
>
>
  • accessed the OS name . We used the System.getProperty method to pass the os.name string as the argument.
  • also accessed OS version and OS architecture through the os.version and os.arch system properties respectively.
  • use java.version to access Java version.
  • use java.home to access Java installation.
  • use user.name to access user account name.
class GetSystemProperties
public static void main( String args[] )
//JRE version number
System.out.println(System.getProperty("java.version"));
//Installation directory for Java Runtime Environment (JRE)
System.out.println(System.getProperty("java.home"));
//User account name
System.out.println(System.getProperty("user.name"));
>
>

Learn in-demand tech skills in half the time

Источник

Detect OS Name and Version Java

Java is Platform independent and can run everywhere. Knowing this, it can be worth knowing in what operation system the application is running. To detect the current operation system, you can use the OSInfo class below, which retrieves the information from the system properties and returns an OS enum that holds the name and version of the operating system the application is running on.

Detect Operating System

We can get the current operation system name by calling System.getProperty(«os.name»); . We evaluate this value and appropriately map it to the correct os. Finally we add the version number of the os by calling the System.getProperty(«os.version»); .

package com.memorynotfound.file; import java.io.IOException; import java.util.Locale; public class OSInfo < public enum OS < WINDOWS, UNIX, POSIX_UNIX, MAC, OTHER; private String version; public String getVersion() < return version; >public void setVersion(String version) < this.version = version; >> private static OS os = OS.OTHER; static < try < String osName = System.getProperty("os.name"); if (osName == null) < throw new IOException("os.name not found"); >osName = osName.toLowerCase(Locale.ENGLISH); if (osName.contains("windows")) < os = OS.WINDOWS; >else if (osName.contains("linux") || osName.contains("mpe/ix") || osName.contains("freebsd") || osName.contains("irix") || osName.contains("digital unix") || osName.contains("unix")) < os = OS.UNIX; >else if (osName.contains("mac os")) < os = OS.MAC; >else if (osName.contains("sun os") || osName.contains("sunos") || osName.contains("solaris")) < os = OS.POSIX_UNIX; >else if (osName.contains("hp-ux") || osName.contains("aix")) < os = OS.POSIX_UNIX; >else < os = OS.OTHER; >> catch (Exception ex) < os = OS.OTHER; >finally < os.setVersion(System.getProperty("os.version")); >> public static OS getOs() < return os; >>

Detecting the current os the application is running on.

package com.memorynotfound.file; public class DetectOS < public static void main(String. args)< System.out.println("OS: " + OSInfo.getOs()); System.out.println("OS version: " + OSInfo.getOs().getVersion()); System.out.println("Is mac? " + OSInfo.OS.MAC.equals(OSInfo.getOs())); >>
OS: MAC OS version: 10.10.5 Is mac? true

References

Источник

Оцените статью