Funny, this seems about five years late, but using Java, if you want to get the HOSTNAME
on Windows Server 2003 (and possibly any version of Windows 2000), you have to do a little extra work. The environment variable you need to access on those versions of Windows is referred to as COMPUTERNAME
, so in my case, since my software is going to be running on a lot of different (and currently-unknown) computer systems, I created a little convenience method to handle this problem.
Here's the source code for a Java method which returns the "host name" for a variety of different computer systems, with specific, explicit support for earlier versions of Windows:
public static String getHostname() { String hostname = System.getenv().get("HOSTNAME"); if (hostname!=null && !hostname.trim().equals("")) return hostname; // support windows server 2003 hostname = System.getenv().get("COMPUTERNAME"); if (hostname!=null && !hostname.trim().equals("")) return hostname; return "UNKNOWN-HOST"; }
Off the shelf this seems to work fine on Linux, Mac OS X, and more modern Windows operating systems, so the only computer platform I've really had to account for is the Windows Server 2003 system I ran into last week.