java

How to check operating system in Java

In this tutorial, we are going to see how to detect your computer’s operating system using the Java programming language.

You can use the Java System.getProperty(“os.name”) class to detect the type of operating system (OS) you are using.
 

Java Program to check operating system:
public class Main {	
	public static void main(String[] args) {
		
		String myOs = System.getProperty("os.name").toLowerCase();
		System.out.println(myOs);
		
		if (myOs.indexOf("win") >= 0) {
			System.out.println("Your system is Windows");
		} else if (myOs.indexOf("mac") >= 0) {
			System.out.println("Your system is Mac");
		} else if (myOs.indexOf("nux") >= 0) {
			System.out.println("Your system is Unix or Linux");
		} else if (myOs.indexOf("sunos") >= 0) {
			System.out.println("Your system is Solaris");
		} else {
			System.out.println("Your system is not supported!");
		}
	}
}

Output:

linux
Your system is Unix or Linux
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *