How do I change the JAVA_HOME for ant?

a 夏天 提交于 2019-12-12 07:39:29

问题


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

回答1:


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




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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




回答6:


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.




回答7:


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 that path is usually taken if Ant is started from another tools like Maven.

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




回答8:


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/




回答9:


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.




回答10:


When using Bash just try this:

$ export JAVA_HOME=/usr/tomcat/jre



回答11:


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"




回答12:


try with this:

/usr/sbin/update-alternatives --config java



回答13:


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



回答14:


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.



来源:https://stackoverflow.com/questions/573235/how-do-i-change-the-java-home-for-ant

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!