33

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
mtk
  • 13,221
  • 16
  • 72
  • 112
Eugene M
  • 47,557
  • 14
  • 38
  • 44
  • echo $ANT_HOME returns nothing so probably not. I'll look into it but any information would be useful. – Eugene M Feb 21 '09 at 20:04

14 Answers14

13

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
  • 2,081
  • 2
  • 15
  • 20
6

On my Windows 7 machine setting:

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

didn't work. But setting:

JAVA_HOME=C:\Program Files\Java\jdk1.6.0_18

worked.

klana
  • 61
  • 1
  • 1
6

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
  • 60,850
  • 18
  • 118
  • 163
  • I think you mean $JAVA_HOME/bin/ (forward slashes) – wds Feb 21 '09 at 16:30
  • ack - my students use windows too much :-) Thanks! – TofuBeer Feb 21 '09 at 16:35
  • 1
    You are correct; however, the "ant.java.home" property will *always* point to a jre because it fleshes out that property from java.lang.System (which by definition is the JRE under the JDK). To definitively set the Java environment in ant, specify java locations using ${env.JAVA_HOME} with exact paths. – ingyhere Aug 02 '13 at 14:24
6

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
  • 28,320
  • 5
  • 53
  • 50
5

There are 2 ways of changing the compiler:

  • export JAVA_HOME=/path/to/jdk before you start Ant.
  • Set <javac exectuable="/path/to/javac">

Another option would be to add a respective tools.jar to the classpath, but this is usually used if Ant is started from another tools like Maven.

For more details on these (or other) options of changing Java Compiler in Ant, see this article for example.

Stanislav Bashkyrtsev
  • 14,470
  • 7
  • 42
  • 45
3

Set the env var: JAVACMD - full path of the Java executable. Use this to invoke a different JVM than JAVA_HOME/bin/java(.exe).

Reference: http://ant.apache.org/manual/running.html

avi
  • 31
  • 1
3

Though the environment variable JAVA_HOME set correctly, the ant may use the configured JRE within the each build.xml or any build files.

To check what version of the JRE the ant is using, right click on the build file -> select the build ant which displays the details about the tasks to choose etc, select the JRE which you want to use.

Its advisable to use the project level settings or just at the workspace level.

mtk
  • 13,221
  • 16
  • 72
  • 112
Niranjan Sarvi
  • 899
  • 7
  • 9
1

When running ant from the shell, you don't need to export JAVA_HOME first, which would set that variable for your current shell and all future commands, instead use

user@host:~# JAVA_HOME=/path/to/jdk ant targets
Alex
  • 10,470
  • 8
  • 40
  • 62
1

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
  • 5,253
  • 7
  • 43
  • 52
1

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
  • 1,157
  • 10
  • 14
  • Read that error message more carefully: Unable to locate tools.jar. Expected to find it in /usr/tomcat/lib/tools.jar That 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 Feb 22 '09 at 02:15
  • @laz it doesn't really matter - the root cause is that OP is trying to use JRE to run Ant, whereas he should be using a JDK, like pointed out by nbeyer – eis Sep 06 '16 at 05:41
1

When using Bash just try this:

$ export JAVA_HOME=/usr/tomcat/jre
cdecker
  • 4,515
  • 8
  • 46
  • 75
0

try with this:

/usr/sbin/update-alternatives --config java
jacktrade
  • 3,125
  • 2
  • 36
  • 50
0

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
  • 929
  • 1
  • 12
  • 27
-1

java_home always points to the jdk, the compiler that gave you the classes, and the jre is thw way that your browser or whatever will the compiled classes so it must have matching between jdk and jre in the version.

gary
  • 4,227
  • 3
  • 31
  • 58
jean
  • 1
  • 1
    I edited out your email per this question on meta: http://meta.stackexchange.com/questions/89965/what-to-do-when-questioner-posts-email-address-for-responses In general this also goes for answers. – gary Sep 20 '11 at 13:52