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,
- How to install Java 11 on Mac
- Get Client IP address from HTTP Response in Java
- SharePoint Open in the client application document opens in browser
- How to verify if java is installed on the computer and get version detail
- Java - Check if array contains the value
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- How to run Java Unit Test cases with Apache Maven?
- What Java version is used for Minecraft 1.18
- List of jar files for Jax-ws (SOAP) based Java Web Services
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- Convert Instant timestamp into LocalDateTime Java Code Example
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Create simple struts2 project using maven commands
- Java - PatternSyntaxException
- List of Online Java compiler with console
- Minecraft Java Edition
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- Java XML-RPC 3.1.x based web service example
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server
- Java 8 foreach loop code examples
- Java -Day of the week using Java 8 DayOfWeek Enum
- Java 8 - Convert List to Map Examples
- Java: TimeZone List with GMT/UTC Offset
- list of jars required for hibernate 4.x.x
More Posts:
- Python matplotlib segmentation fault: 11 macOS Big Sur - Python
- 2020 Popular Halloween Emoji's and hashtags - Hashtags
- Notepad++ insert a blank line above or below the current line example - NotepadPlusPlus
- How to empty trash in Android Device - Android
- Use 5G Network on Android Emulator - Android
- How to check uptime of Linux/Unix/macOS system/server using console command? - Linux
- Bypass domain name with credentials SharePoint, .Net site from IIS - SharePoint
- MySQL : Error :1004 SQLSTATE: HY000 (ER_CANT_CREATE_FILE) Can't create file - MySQL