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?
One way is to make use of the System. getProperty(os.name) to obtain the name of the operating system. The second way is to make use of SystemUtils from the Apache Commons Lang API.
Use the System. getProperty() method in Java to get the Operating System name and version.
In theory yes, but you'll still have to have some low-level assembly code to bootstrap the Java VM that will run on the machine, and also low-level code for accessing hardware drivers.
You can use:
System.getProperty("os.name")
P.S. You may find this code useful:
class ShowProperties { public static void main(String[] args) { System.getProperties().list(System.out); } }
All it does is print out all the properties provided by your Java implementations. It'll give you an idea of what you can find out about your Java environment via properties. :-)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With