Month: November 2016

How to detect OS in Java

In previous post, we have seen couple of ways to determine Operating system using Python -> Determine Operating System using Python

In this post we will try to do that using Java.

One of the system properties, os.name can help us in achieving that.

This code can detect Windows, Mac, Unix, Solaris.

public class DetermineOS {

public static String OS = System.getProperty(“os.name”).toLowerCase();

public static void main(String[] args){

System.out.println(OS);

if(OS.contains(“win”)){

System.out.println(“Windows Environment”);

}else if(OS.contains(“mac”) {

System.out.println(“Mac Environment”);

}else if(OS.contains(“sunos”)){

System.out.println(“This is Solaris”);

}else if(OS.contains(“nix”) || OS.contains(“nux”) || OS.contains(“aix”)){

System.out.println(“This is Unix environment”);

}else {

System.out.println(“Your OS is not supported”);

}

}

}

Please comment if you are aware of any other ways to determine Operating system.