tags:

views:

9239

answers:

8

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

+4  A: 

JAVA_HOME should point at where the JDK is installed not not a JRE.

So, if you type ls $JAVA_HOME what do you see? if you do ls $JAVA_HOME/bin/ do you see javac?

If the first doesn't work then you don't have JAVA_HOME pointing at the right directory. If the second doesn't work then you need to point JAVA_HOME at a JDK instead of a JRE.

TofuBeer
I think you mean $JAVA_HOME/bin/ (forward slashes)
wds
ack - my students use windows too much :-) Thanks!
TofuBeer
+2  A: 

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

Nils Weinander
using the `jre` wont work; it does not contain the tools which ant is asking for
Carlos Heuberger
@Carlos Heuberger: quite right, I have corrected that
Nils Weinander
+2  A: 

Looking at the shell script for invoking ant, it is possible that the value for $JAVA_HOME set for your shell in .bashrc can be overridden in the files /etc/ant.conf, $HOME/.ant/ant.conf, and $HOME/.antrc. If you execute bash -x <path to ant script> it will emit debugging information which should help you track down where $JAVA_HOME is being overridden.

laz
+1  A: 

JAVA_HOME needs to point to a JDK home if you're trying to compile code. Check to see if '/usr/tomcat/jre/bin/javac' exists. I doubt it does.

If you don't have a JDK, then you can work around it by getting the ECJ (eclipse compiler) library, dropping it into '~/.ant/lib' and adding a system property to the command-line to use that compiler - check the Ant manual for details.

http://ant.apache.org/

nbeyer
Read that error message more carefully: Unable to locate tools.jar. Expected to find it in /usr/tomcat/lib/tools.jarThat indicates that JAVA_HOME is actually /usr/tomcat instead of /usr/tomcat/jre. For some reason the an incorrect value for JAVA_HOME is overriding the shell value.
laz
A: 

When using Bash just try this:

$ export JAVA_HOME=/usr/tomcat/jre

HTH, Snyke

cdecker
A: 

You will need to change JAVA_HOME path to the Java SDK directory instead of the Java RE directory. In Windows you can do this using the set command in a command prompt.

e.g.

set JAVA_HOME="C:\Program Files\Java\jdk1.6.0_14"

PropellerHead
A: 

On my Windows (7) machine set JAVA_HOME="C:\Program Files\Java\jdk1.6.0_18" doesn't work. But set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_18 worked.

klana
A: 

Set the JRE in the project (project properties -> Java Build Path-> Libraries, typically last entry), or global default in preferences (Java->Installed JREs) to a JDK, not a JRE.

stolsvik