How to Determine Operating System in Java


In this example, you may see how to determine the operating system JVM is running on.

Source Code

package com.beginner.examples;


public class DetermineOSExample {

    public static void main(String[] args){
    	
    	String osName = System.getProperty("os.name");		 
    	if (osName.startsWith("Mac OS")) 
    	{
            // Mac OS
    		System.out.println("Mac OS");
        } 
    	else if (osName.startsWith("Windows")) 
    	{
            // Windows
        	System.out.println("Windows");
        } 
    	else 
    	{
            // other
        	System.out.println("other");
        }
    }
    
}

Output:

Windows
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments