tags:

views:

7523

answers:

6

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?

+18  A: 

You can use:

System.getProperty("os.name")

P.S. You may find this code useful.

import java.util.Map;

class ShowProperties {
    public static void main(String[] args) {
        for (Map.Entry<Object, Object> e : System.getProperties().entrySet()) {
            System.out.println(e);
        }
    }
}

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

P.P.S. (For pedants only.) I realise that java.util.Properties has a toString method, and thus the property iteration is redundant; however I don't like its formatting (I prefer one property a line, thanks). Just in case people ask.

Chris Jester-Young
Careful!!! That smiley is not a legal java operator!!!Though I think it should instruct the compiler to "please accept this code. I tried my best."
benjismith
It's not, but it's not in the backticks code block either. :-)
Chris Jester-Young
sorry 'bout the semicolon
jjnguy
It's okay, I understand that typing a semicolon is a habit for many programmers, myself included.
Chris Jester-Young
+10  A: 
System.getProperty("os.name")
Julien Grenier
+1  A: 

I would recommend to cache it in a static variable:

public static final class OsUtils
{
   private static String OS = null;
   public static String getOsName()
   {
      if(OS == null) { OS = System.getProperty("os.name");
   }
   public static boolean isWindows()
   {
      return getOsName().startsWith("Windows");
   }

   public static boolean isUnix() // and so on
}

That way, every time you ask for the Os, you do not fetch the property more than once in the lifetime of your application.

VonC
I agree with the getOSName function, on the basis of OAOO (once and only once); however, the caching is totally redundant given the speed of hash lookups.
Chris Jester-Young
Totally redundant might be a bit harsh, hash lookups are more expensive than accessing a reference. It all depends on the context.
Craig
Good points... Feel free to down-vote if you think it is a bad practice ;)
VonC
+1  A: 

If you're interested in how an open source project does stuff like this, you can check out the Terracotta class (Os.java) that handles this junk here:

And you can see a similar class to handle JVM versions (Vm.java and VmVersion.java) here:

Alex Miller
+1  A: 

I find that the OS Utils from Swingx does the job.

Richard Harrison
The above link seems to be broken, probably due to SwingX introducing branches; the 1.6 release is here: https://swingx.dev.java.net/source/browse/swingx/tags/SwingX-1-6/src/java/org/jdesktop/swingx/util/OS.java?view=markup
David Moles
@David Moles, thanks. the link was ok when I answered- I've now updated it with your one.
Richard Harrison
A: 

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.

Leif Carlsen