Get OS version, name, architecture, and system properties in Java
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 nameSystem.out.println("Your OS name -> " + System.getProperty("os.name"));//Operating system versionSystem.out.println("Your OS version -> " + System.getProperty("os.version"));//Operating system architectureSystem.out.println("Your OS Architecture -> " + System.getProperty("os.arch"));}}
In the code above, we:
-
accessed the
OS name. We used theSystem.getPropertymethod to pass theos.namestring as the argument. -
also accessed
OS versionandOS architecturethrough theos.versionandos.archsystem properties respectively.
In addition, we can:
-
use
java.versionto access Java version. -
use
java.hometo access Java installation. -
use
user.nameto access user account name.
class GetSystemProperties {public static void main( String args[] ) {//JRE version numberSystem.out.println(System.getProperty("java.version"));//Installation directory for Java Runtime Environment (JRE)System.out.println(System.getProperty("java.home"));//User account nameSystem.out.println(System.getProperty("user.name"));}}