How do I change the JAVA_HOME for ant? - ant

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

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

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.

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.

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.

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.

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

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.

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/

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.

When using Bash just try this:
$ export JAVA_HOME=/usr/tomcat/jre

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

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"

try with this:
/usr/sbin/update-alternatives --config java

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.

Related

"JUnitTask was not found" error (Hudson, Ant, JUnit)

I tried several ways to install ant and junit on the same server as hudson. I eventually downloaded the Ant source, installed the optional dependencies by using the included fetch.xml file, built the source code, and dropped junit-4.8.1.jar into the lib directory where the compiled ant code [that I just built] is. Then I set ANT_HOME to the ant root directory by using the "export ANT_HOME=/usr/share/ant" command. I also set a Hudson configuration variable of "ANT_HOME" with the same value (/usr/share/ant) and I also put a new file into the /etc/profile.d directory with the export ANT_HOME command as well. At this point I'm not sure what else to try, but Hudson still gives me the error:
BUILD FAILED /var/lib/hudson/jobs/MyProject/build.xml:31: Problem:
failed to create task or type junit Cause: the class
org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found.
This looks like one of Ant's optional components. Action: Check that the appropriate optional JAR exists in
-/usr/share/ant/lib
-/var/lib/hudson/.ant/lib
-a directory added on the command line with the -lib argument
Do not panic, this is a common problem. The commonest cause is a
missing JAR.
This is not a bug; it is a configuration problem
I did, in fact, check /usr/share/ant/lib and the class "org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.class" is located in the ant-junit jar file as I confirmed by running the command: jar tfv ant-junit.jar | grep org.apache.tools.ant.taskdefs.optional.junit.JUnitTask
Can someone please tell me what to try, I am not sure where to go from here.
If you are on Ubuntu you can simply apt-get ant; I did that and linked to the junit jar within my project.
Cheers,
Neil
It turns out that hudson itself installs some version of Ant in one of its own libraries. I suppose that somehow caused a conflict because when I got rid of it, things worked. I think it is in /var/hudson or /var/hudson/lib. That path is added to the PATH environment variable during hudson installation. So your choice is either to add your own Ant install to the BEGINNING of the path, to delete hudson's version of Ant, or to delete that part of the PATH.

in ant :BUILD EXCEPTION

I am getting some problem while running ant
Here is the details of problem:
C:\jboss-5.1.0.GA\server\default\deploy\mdd_install\mc_config\ant\compile\compile.xml:30: 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
at org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory.getCompiler(CompilerAdapterFactory.java:105)
at org.apache.tools.ant.taskdefs.Javac.compile(Javac.java:924)
at org.apache.tools.ant.taskdefs.Javac.execute(Javac.java:757)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
I echo java_home and its value is correct
I am running my aplication from jboss and try to execute it, but it is getting the above exception. I put tools.jar in jboss server lib folder ,with that it is runnig fine,but it is not correct way.
Make sure the case of the path to JAVA_HOME is right. Reopen the window or environment you're running ant from to make sure it's using the updated environment vars.

Ant build tool installation not working properly!

i have downloaded the Ant distribution from the Ant web site.
apache-ant-1.7.0
and set environment variable specifying the location of the Ant installationas
ANT_HOME = C:\portal\apache-ant-1.7.0
PATH=;%ANT_HOME%\bin
But ant is not getting properly installed
Better use batch or shell scripts to start your ant files, f.e. =
windows
set JAVA_HOME=C:\java\jdk\1.6.0_xx
set ANT_HOME=C:\ant
set ANT_ARGS=-lib C:\ant_xtralibs;C:\ant_testlibs
set PATH=%PATH%;%JAVA_HOME%\bin;%ANT_HOME%\bin;C:\cvsnt
:: default
call ant -f %1
:: debug
::call ant -debug -f %1
...
unix - don't forget the quotationmarks on the ANT_ARGS line !
...
ANT_ARGS="-lib /usr/local/ant_xtralibs:/usr/local/ant_testlibs"
export ANT_ARGS
...
Some advantages of starting ant like that :
ANT_ARGS is a special environment variable. Its content is automatically
added to the invocation of ant.
you may use your own ant settings on a machine where you have no admin rights
using a separate folder for your ant addon libs and load via -lib option
keeps your ant installation clean and avoids polluting the %ANT_HOME%/lib folder
Move the ';' to the end. Be sure that java is also correct installed (variable for classpath, etc.)
Depending on you verson of windows (win2k & win XP), you might need to reboot your computer to make your PATH modifications taken into account.

Installing Ant on Cygwin

I'm having some trouble figuring out how to install Ant on Cygwin. I want to use Ant to build Nutch. I've looked through a bunch of tutorials but I can't find anything that is low level enough for me to understand. I need something like...
Download ant, put it here
Open Cygwin
type "export ANT_HOME=..."
...
Can anyone help me out here?
Assuming you have a JDK already installed, you can do this:
$ export ANT_HOME=/cygdrive/c/apache-ant-1.7.1
which assumes you've unzipped Ant into C:\apache-ant-1.7.1. Then:
$ export PATH=$ANT_HOME/bin:$PATH
$ ant -version
Apache Ant version 1.7.1 compiled on June 27 2008
In Windows, add the path to your ant /bin directory to the Path system variable. This can easily be done by right clicking on Computer > Properties > Advanced System Settings > Environment Variables, click on Path in the System Variables, click on Edit and add ; followed by the path to your ant bin directory to the end of the Variable value.
Start or restart Cygwin.
Type ant -version The version should be displayed.
Here's a step-by-step guide:
simply download and unzip ANT binaries say into c:\apache-ant-1.8.1
download and unzip NUTCH sources say into: c:\apache-nutch-1.2
open the command prompt and run the following:
cd c:\apache-nutch-1.2
c:\apache-ant-1.8.1\bin\ant
the same would work from the Bash shell, just use Cygwin-style paths:
cd /cygdrive/c/apache-nutch-1.2
./cygdrive/c/apache-ant-1.8.1/bin/ant
That's it, you will find a new directory build containing the output.
For convenience, you might want to add the Ant bin directory to the PATH environment variable so that you don't have to give the full path each time, but that's optional.
BTW I just did those exact steps, and all went fine.
Finally, follow this tutorial to get started.

Why does ANT tell me that JAVA_HOME is wrong when it is not?

I get the error:
C:\dev\ws\springapp\build.xml:81: 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.
It is currently set to "C:\Program Files\Java\jre6"
But I have clearly set my JAVA_HOME to be C:\Program Files\Java\jdk1.6.0_14
Where is ANT getting this value?
In Eclipse click Run → External Tools → External Tools Configurations.
Click the JRE tab.
Click the Installed JREs... button.
Click the Add button.
(Select Standard VM, where applicable.)
Click the Directory button.
Browse to your JDK version (not JRE) of your installed Java
(e.g. C:\Program Files\Java\jdk1.7.0_04).
Click Finish and OK.
Select the JDK at Separate JRE and click Close.
Re-run your Ant script — have fun!
This worked in a particular scenario I encountered.
When you say you have "clearly set" JAVA_HOME to "C:\Program Files\Java\jdk1.6.0_14" - is that what you see when you run "set" from the command line? I believe Ant will guess at a value for JAVA_HOME if one isn't set at all... is it possible that you've set JAVAHOME instead of JAVA_HOME?
If it's nothing like that, I suggest you edit the ant.bat batch file (and whatever it calls - I can't remember whether it's convoluted or not offhand) to print out JAVA_HOME at the start and at various other interesting places.
I encountered the same problem when I try to run Ant build using the following command:
java -cp ant.jar:ant-launcher.jar org.apache.tools.ant.Main
The output of the command is:
BUILD FAILED
XXX/build.xml:8: 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.
It is currently set to "/usr/lib/jvm/java-6-openjdk/jre"
It seems the java executable is picking "/usr/lib/jvm/java-6-openjdk/jre" as JAVA_HOME, as opposed to "/usr/lib/jvm/java-6-openjdk" where JDK is installed.
I resolved this issue by setting fork="yes" in my <javac> task.
Take a look at: http://ant.apache.org/manual/Tasks/javac.html
It's also possible that you have included /bin in your JAVA_HOME setting, and Ant is adding /bin to it - thereby not finding any exe's. It's happened to me :}
Make sure you do not use the trailing semicolon: This will not work:
set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_29;
This will:
set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_29
I have met the similiar issue. I would link to run Ant task fron Maven build and I got the issue. I have fixed it as bellow steps:
Make sure JAVA_HOME is set right. You can check it on Windowns in command line as:
C:>echo %JAVA_HOME%
Result would look like: C:\Progra~1\Java\jdk1.6.0_30\
Copy file tools.jar from %JAVA_HOME%\lib to lib directory of Maven.
And it worked for me.
To solve this problem add tools.jar file in window->preferences->ant-> runtime .
In eclipse set the installed JRE setting to the JDK - in the project (project properties -> Java Build Path-> Libraries), or global default in preferences (Java->Installed JREs). The eclispe setting is stronger than the system variable.
I hope you are aware about System and User environmental variables. The user ones are preferred over system. If you have set your JAVA_HOME in system variables and if there is an entry for the same in user variables, then you will get the latter one only.
Right click on My computer, Go to properties, Select Advanced tab and click on Environmental variables to see the list of user and system environment variables.
The semicolon was throwing me off: I had JAVA_HOME set to "C:\jdk1.6.0_26;" instead of "C:\jdk1.6.0_26". I removed the trailing semicolon after following Jon Skeet's suggestion to examine the ant.bat file. This is part of that file:
if "%JAVA_HOME%" == "" goto noJavaHome
if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
So the semi-colon wasn't being trimmed off the end, causing this to fail to find the file, therefore defaulting to "C:\Java\jre6" or something like that.
The confusing part is that the HowtoBuild page states to use the semi-colon, but that seems to break it.
I was facing the same problem, making the executable property in the javac tag to be set to the location of javac.exe resolved the problem for me.
This resolved the problem
<javac srcdir="${srcDir}" destdir="${buildDir}" fork="true" executable="C:\Program Files\Java\jdk1.7.0_03\bin\javac"/>
Actually found this answer on superuser.com, but I had to copy tools.jar from my JDK\lib directory to the JRE\lib directory.
Makes ZERO sense...only thing I can think of is Sun introduced this bug in the latest Java runtime (Java 7 Update 11) or a bug in Ant in how it reads the current JDK location (the JRE is more updated than the JDK obviously which is also stupid of Sun...they should release the JDK each time they update the JRE).
My JAVA_HOME was set correctly. I confirmed by doing "set JAVA_HOME". It pointed to my JDK directory and was spelled correctly. However, Ant was claiming it couldn't find javac, but thought JAVA_HOME was in my JRE directory.
My system worked fine before the latest Sun JRE7 updates (10 and 11). Ant is version 1.8.4
If you have JAVA_HOME set but there's a typo in it, you will also see the bogus reference to a jre6 path.
When everything else is correct try the following
Goto Window -> Preferences -> Ant -> Runtime -> ClassPath
Expand Global Entries and check jdk tools available here.
Add the required tools.jar from a specific version of java such as "C:\Program Files\Java\jdk1.6.0_45\lib\tools.jar"
Now build and check the results.
Note: Add tools.jar for the java compiler that you want to use and give preference to it by moving it up in the list of tools.jar when multiple tools.jar (java compilers) are present.
Just set the JDK path from the below steps:
Go to 'Run' ->External Tools -> External Tools Configuration
-> Select 'JRE'
Then from the DropDown list select the available JDK . ex. jdk1.8.0_25
I had the same problem. My JDK package pointed by JAVA_HOME didn't have any tools.jar
Be sure that your JDK instal.lation has tools.jar
(clearly the message error is confusing)
Set JAVA_HOME in the environment variables as D:\Program Files\IBM\SDP\jdk
Do not give any quotes or semicolon. It's works for me.Please try the solution.
Actually in ant.bat it checks for appropriate JAVA_HOME in case if ant.bat not able to find it then it's JAVA_HOME points the default JRE.
I was also facing the same problem. I am using Windows 7 and I had two versions of java installed. First I have installed latest version java 7 and then version 5.
Contents of my java installation directory:
C:\Program Files\Java>
jdk1.5.0_14
jdk1.7.0_17
jre1.5.0_14
jre7
and my JAVA_HOME was set to the correct value, which was:
C:\>set ja
JAVA_HOME=C:\Program Files\Java\jdk1.5.0_14
But still I was getting the same problem:
XXXXXXX\build.xml:478: The following error occurred while
executing this line:
XXXXXXX\build.xml:477: 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.
It is currently set to "C:\Program Files\Java\jre7"
After trying out all the suggestion in this thread I realized my mistake. I was trying to set the environment variable in "User variables" instead of "System Variables" section. After setting it in "System Variables" it worked fine. I am facing another problem though.
The default version of java it points to is still 7.
C:\>java -version
java version "1.7.0_17"
Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
Java HotSpot(TM) Client VM (build 23.7-b01, mixed mode, sharing)
I am not sure how to make it point to version 5.
I faced this problem when building my project with Jenkins. First, it could not find ant.bat, which was fixed by adding the path to ant.bat to the system environment variable path. Then ant could not find the jdk directory. This was fixed by right-clicking on my computer > properties > advanced > environment variables and creating a new environment variable called JAVA_HOME and assigning it a value of C:\Program Files\Java\jdk1.7.0_21. Don't create this environment variable in User Variables. Create it under System Variables only.
In both cases, I had to restart the system.
It is common to get this issue. I cannot set any specific Java home in my system as I have 2 different version of Java (Java 6 and Java 7) for different environment.
To resolve the issue, I included the JDK path in the run configuration when opening the build.xml file.
This way, 2 different build files use 2 different Java version for build.
I think there might be a better solution to this problem but at least the above approach avoid setting the JAVA_HOME variable.
FYI, I am using Windows 7 and had to restart Windows in order for the new JAVA_HOME setting to take effect.
had the same problem;
c:\jb\src\build.xml:191: 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.
It is currently set to "C:\Program Files\Java\jre7"
Total time: 0 seconds
c:\jb\src>echo %JAVA_HOME%
c:\Program Files\Java\jdk1.7.0_13
solution:
path = c:\ant\bin\;%PATH%
path = c:\Program Files\Java\jdk1.7.0_17\bin;%PATH%
set "JAVA_HOME=c:\Program Files\Java\jdk1.7.0_13"
I guess the jdk path instruction alone would do it but I'm to lazy to figure it out above solution is bullet proof. :-)
Also using Windows7
Quick work around for the same is
Copy C:\Program Files\Java\jdk1.6.0_14\lib\tools.jar to C:\Program Files\Java\jre6\lib\
This exception is coming because JAVA_HOME is being set as C:\Program Files\Java\jre6 and Ant is not able to find tools.jar in it.
If need to run ant in eclipse with inbuilt eclipse jdk add the below line in build.xml
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>

Resources