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,
- Drop table using Java JDBC Template
- Java - Check if array contains the value
- YAML Parser using Java Jackson Library Example
- Java Jackson ObjectMapper Class with Examples
- Get Client IP address from HTTP Response in Java
- How to Word-Warp Console logs in IntelliJ
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Setting Java_Home Environment variable on Windows Operating System
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Java SE JDBC Select Statement Example
- How to extract Java Jar/War/Ear files in Linux
- java: unclosed string literal [Error]
- [Solution] Exception in thread main java.util.EmptyStackException
- Read YAML file Java Jackson Library
- What Java version is used for Minecraft 1.18
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Program] How to read three different values using Scanner in Java
- Java 8 Predicate Functional Interface Examples
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Convert String Date to Date Object in Java
- Struts 2 Hello World Example in Eclipse
- Read a file using Java 8 Stream
- Java - How to set custom thread name?
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- java: ']' expected error [fixed]
More Posts:
- Eclipse like Auto Import Shortcut in Intelij IDE Android Studio - Android-Studio
- Resolve - zsh: command not found: code - zsh
- Setting Java_Home Environment variable on Windows Operating System - Java
- Access URL for SharePoint Tenant Admin Center (Online Office 365) - SharePoint
- [Android Studio] Button on click example - Android-Studio
- How to resolve Certificate Expired WhatsApp Error - WhatsApp
- How to see HTTP Request Response Headers in Google Chrome Browser - Chrome
- Java: Create Temporary Directory and File and Delete when application terminates - Java