How do I get the hostname in a cross-platform way?

Erik Hatcher

Ant has a mechanism for gathering environment variables. Unfortunately the hostname environment variable is different on Windows versus Unix boxes, but Ant's property immutability helps us out:

  <target name="set-name">
    <property environment="env"/>
    <property name="env.HOSTNAME" value="${env.COMPUTERNAME}"/>
    <echo message="host = ${env.HOSTNAME}"/>
HOSTNAME is the environment variable on Unix platforms (at least on the ones I've used - please alert me if this is not always the case), and COMPUTERNAME on Windows platforms.