How to determine the OS the computer is running using Java?


The System class of java.lang package provides a method named getProperty() this method accepts one of the following string parameters an returns the respective property.

  • java.class.path − If you pass this value as a parameter, the getProperty() method returns the current classpath.

  • java.home − If you pass this value as a parameter, the getProperty() method returns the current Installation directory of the JRE.

  • java.vendor − If you pass this value as a parameter, the getProperty() method returns the current vendor name of the JRE.

  • java.vendor.url − If you pass this value as a parameter, the getProperty() method returns the current vendor URL of the JRE.

  • java.version − If you pass this value as a parameter, the getProperty() method returns the current version number of the JRE.

  • line.separator − If you pass this value as a parameter, the getProperty() method returns the value/sequence used by the current os to separate the lines in text files.

  • file.separator − If you pass this value as a parameter, the getProperty() method returns the character used to separate the directories in a file path (/ Or \).

  • path.separator − If you pass this value as a parameter, the getProperty() method returns the path separator of the classpath environmental variable.

  • user.dir − If you pass this value as parameter the getProperty() method returns the working directory of the user.

  • user.home − If you pass this value as parameter the getProperty() method returns the home directory of the user.

  • user.name − If you pass this value as parameter the getProperty() method returns the account name of the user.

  • os.arch − If you pass this value as parameter the getProperty() method returns the architecture of the current operating system.

  • os.name − If you pass this value as parameter the getProperty() method returns the name of the current operating system.

  • os.version − If you pass this value as parameter the getProperty() method returns the version of the current operating system.

To get the name of the current operating system invoke the getProperty() method by passing the value "os.name" to it as shown in the following sample code.

Example

Live Demo

public class Sample {
   public static void main(String args[]) {
      String os = System.getProperty("os.name");
      System.out.println("Current Os is: "+os);    
   }  
}

Output

Current Os is: Windows 10

Updated on: 30-Jul-2019

90 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements