'Apache Ant is not able to find environment variables from Windows

I have an ant build file in which I am trying to use an executable referenced in an environment variable, but Ant is not able to find it.

I echoed the value of the PATH variable and find that it is not actually getting anything. I am not sure why.

Below are the code sample and output.

Ant build file:

 <target name ="cmd-local">
    <echo message="$PATH is set to = ${PATH}" />
    <echo message="%PATH is set to = %PATH%" />
    <exec executable="cmd.exe">
        <arg line="/c echo %PATH%" />  
     </exec>
 </target>

Output:

[echo] $PATH is set to = ${PATH}
[echo] %PATH is set to = %PATH%
[exec] C:/Users/user1/Application/jdk1.8.0_121/bin/..

I am not sure why the first two are not printing anything.

ant


Solution 1:[1]

The main problem is that Ant doesn't import the environment into properties by default. You have to ask it do that, and specify a prefix like:

<property environment="env" />

That will load environment variables into properties with names like env.PATH, hence you would access PATH with something like:

<echo message="$PATH is set to = ${env.PATH}" />

In Ant there's nothing special about Windows-style environment references like %PATH%, they are just treated as literal strings.

Solution 2:[2]

Got the same issue, you should consider trying this (case-sensitive)

<echo message="$PATH is set to = ${env.Path}" />

This worked for me (on ant 1.9.5)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 martin clayton
Solution 2 AndyDev