${env.JAVA_HOME} not found - Ant

33,397

Solution 1

The message tells you that Ant was not able to resolve the property env.JAVA_HOME; this means that the environment variable JAVA_HOME was not set in that machine.

Solution 2

You can usually find on your system (if you're Unix) where the actual ant command lives by doing either which ant or type ant. If you look at that location, you will usually see that it's a link to the actual ant command under the $ANT_HOME directory.

Take a look at this script. Much of it is just trying to determine exactly where $ANT_HOME and $JAVA_HOME reside if these are not set by default in the environment.

What you don't see in the ant shell script is:

 EXPORT JAVA_HOME

So, even though $JAVA_HOME is set inside the ant script, it is never exported into the environment (unless someone has modified the ant shell script. If an environment variable is not exported, it is unavailable to child processes -- like thejavachild process running yourant` process.

Thus, if you are on a machine where $JAVA_HOME isn't set before ant is executed, it won't be available in your build script.

However, both Ant and Java (because Ant is a Java process) setup a whole slew of default properties that you can use. When Ant executes it sets it's own built in properties that include things like ${ant.home}. And, when Java is executed, Java also sets up a complete list of Java Properties like ${java.home}.

So, if you really need to know where your JAVA_HOME directory is located, use the property ${java.home} and not depend upon the environment variable $JAVA_HOME.

If you'd like to get a list of these properties, run the following Ant build file:

<project>
    <echoproperties/>
</project>
Share:
33,397
One Two Three
Author by

One Two Three

Updated on July 11, 2020

Comments

  • One Two Three
    One Two Three almost 4 years

    In my build.xml file, I have these lines:

    <property environment="env"/>
    <echo message="JAVA_HOME is set to = ${env.JAVA_HOME}" />
    

    On some machine, this would print

    "JAVA_HOME is set to = /usr/jdk1.6"
    

    But on some others, it would print this

    "JAVA_HOME is set to = ${env.JAVA_HOME}"
    

    Does anyone know what might cause this?

    Thanks

  • One Two Three
    One Two Three almost 11 years
    If JAVA_HOME is not set, then how was it possible for the project to be built? (It's a Java project)
  • smooth reggae
    smooth reggae almost 11 years
    Ant does not require you to set JAVA_HOME (as you may have figured out because you were able to execute your build file even on machines where JAVA_HOME was not set)
  • One Two Three
    One Two Three almost 11 years
    Ok this makes sense now. Thanks
  • Victor
    Victor about 9 years
    This should be marked as correct answer.. as it contains an answer to what the OP was looking for...
  • eis
    eis over 8 years
    well, this is just wrong. ${java.home} is certainly not telling where JAVA_HOME is, only the environment variable will tell that. ${java.home} will tell where JRE used in the execution is, which is a different thing.