If you have a Java Application that is supposed to run on various operating system (Windows, macOS, Linux etc) it becomes very important to know which underlying operating system is the code executing to set options like - directory structure, file system, end of line encoding (CL, RF, CLRF) etc,
Java code to detect Operating System (OS):
/**
*
* Detect Java OS
*
*/
public class Example {
public static void main(String[] args) {
String os = getOperatingSystemDetails();
if(os.contains("Windows")) {
System.out.println("Your Operating System is - Windows");
} else if (os.contains("Mac OS X")) {
System.out.println("Your Operating System is - macOS");
} else if (os.contains("Linux")) {
System.out.println("Your Operating System is - Linux");
} else {
System.out.println("Your Operating System is " + os);
}
}
/**
* This Method returns the underlying OS where
* the Java code run.
*
* @return the Operating System name as a String
*/
public static String getOperatingSystemDetails() {
return System.getProperty("os.name");
}
}
Output:
Your Operating System is - macOS

Detect Underlying Operating System using Java Code
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- 21: Program to Delete File or Folder in Python - Python-Programs
- How to create a Java Project as a Git Repository with IntelliJ - Java
- Fix: Jupyter Notebook: The port 8888 is already in use, trying another port. - Python
- Read a file and Split using StringTokenizer in Java - Java
- [Fix] zsh: command not found: python on Mac - Python
- Facebook | Error : Sorry, something went wrong We're working on it and we'll get it fixed as soon as we can - Facebook
- Know Bash shell version command - Bash
- Python: Sort List in Descending Order - Python