How to change the JAVA_HOME for ant

antjava-home

I'm doing java work on a class server where I don't have root. Whenever I try to compile using ant, it points to the wrong directory (/usr/tomcat instead of /usr/tomcat/jre ).

One of the things we were told to do when setting up our user accounts was to add export JAVA_HOME=/usr/tomcat/jre to the .bashrc file. I don't know if that was supposed to take care of the problem but it doesn't seem to.

So, how can I change the JAVA_HOME property for ant but only for when I run ant?

EDIT:
echo $JAVA_HOME points to /usr/tomcat/jre
echo $JAVA_HOME\bin points to /usr/tomcat/jrebin

The problem is when I normally run ant I get this error:

Unable to locate tools.jar. Expected to find it in /usr/tomcat/lib/tools.jar
Buildfile: build.xml

compile:
    [javac] Compiling 1 source file to /home/ejm244/build/classes

BUILD FAILED
/home/ejm244/build.xml:9: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.

Perhaps JAVA_HOME does not point to the JDK

Total time: 0 seconds

Best Solution

You could create your own script for running ant, e.g. named ant.sh like:

#!/bin/sh
JAVA_HOME=</path/to/jdk>; export JAVA_HOME
ant $@

and then run your script.

$ chmod 755 ant.sh
$./ant.sh clean compile

or whatever ant target you wish to run

Related Question