ant machine name property ant machine name property windows windows

ant machine name property


<exec executable="hostname" outputproperty="computer.hostname"/>

will work on linux and windows


On Windows the hostname is in the environment variable "COMPUTERNAME", on Linux the environment variable is "HOSTNAME". Because ant properties are immutable something like the following should work:

<property environment="env"/><property name="env.HOSTNAME" value="${env.COMPUTERNAME}"/><echo message="hostname = ${env.HOSTNAME}"/>

i.e. import the environment as properties prefixed with env. Then set env.HOSTNAME to be the value of env.COMPUTERNAME unless env.HOSTNAME is already set in which case the 2nd line will have no effect. After that use env.HOSTNAME where the hostname is required.


The correct way to find the local machine's hostname is by using Ant's HostInfo task. This will work across all platforms and is natively supported by Ant.

<hostinfo prefix="host." /><echo message="My hostname is '${host.NAME}'" />