I agree with peterh's answer, so for those of you who like to copy and paste instead of 60 more seconds of Googling:
private String getComputerName()
{
Map<String, String> env = System.getenv();
if (env.containsKey("COMPUTERNAME"))
return env.get("COMPUTERNAME");
else if (env.containsKey("HOSTNAME"))
return env.get("HOSTNAME");
else
return "Unknown Computer";
}
I have tested this in Windows 7 and it works. If peterh was right the else if
should take care of Mac and Linux. Maybe someone can test this? You could also implement Brian Roach's answer inside the else
if you wanted extra robustness.