I would like to determine the operating system of the host that my Java program is running programmatically (for example: I would like to be able to load different properties based on whether I am on a Windows or Unix platform). What is the safest way to do this with 100% reliability?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Let's say you have a Util class for such utility functions. Then create public enums for each operating system type.
Then you can easly invoke class from any class as follows,(P.S. Since the we declared os variable as static, it will consume time only once to identify the system type, then it can be used until your application halts. )
and so on...
This code for displaying all information about the system os type,name , java information and so on.
I think following can give broader coverage in fewer lines
More details here: https://commons.apache.org/proper/commons-exec/apidocs/org/apache/commons/exec/OS.html
As indicated in other answers, System.getProperty provides the raw data. However, the Apache Commons Lang component provides a wrapper for java.lang.System with handy properties like SystemUtils.IS_OS_WINDOWS, much like the aforementioned Swingx OS util.
some of the links in the answers above seem to be broken. I have added pointers to current source code in the code below and offer an approach for handling the check with an enum as an answer so that a switch statement can be used when evaluating the result:
The helper class is: