Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programmatically determine operating system in Java?

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?

like image 201
karlgrz Avatar asked Oct 23 '08 03:10

karlgrz


People also ask

How do I find my operating system in Java programmatically?

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.

How do I find the version of Java OS?

Use the System. getProperty() method in Java to get the Operating System name and version.

Can you code an OS in Java?

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.


2 Answers

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. :-)

like image 94
Chris Jester-Young Avatar answered Oct 11 '22 00:10

Chris Jester-Young


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.

like image 33
Leif Carlsen Avatar answered Oct 11 '22 01:10

Leif Carlsen