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

24

63

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?

Moldau answered 13/10, 2009 at 7:55 Comment(7)
How did you set %JAVA_HOME%? Have you restarted Windows since you made the change?Applicative
I would have thought restarting was not needed now that we have Windows Vista - how hard can it be to register a change with out a restart. But what do you know, that seemed to do the trick.Moldau
Indeed, you should not need to restart Windows in XP and above. But you have to spawn another console...Bucharest
Windows isn't going to change the environment block of already running programs, when you set a new environment variable in the shell.Passionate
Please have a look at Installing Apache Ant. ant.apache.org/manual/install.htmlUniformitarian
I had the same issue. My problem was that I had set JAVA_HOME as a system environment variable instead of a user environment variable.Paschasia
Possible duplicate of Set $JAVA_HOME in eclipse but ANT Cannot Find Java CompilerHalette
S
34

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.

Sigil answered 13/10, 2009 at 7:59 Comment(3)
After hours and hours, this was it. Make absolutely sure that JAVA_HOME is set to C:\Program Files\Java\jdk1.6.0_14 . NOT the jre directory, NOT the bin directoryAlameda
What Christiaan said was the correct way to do it. Also, remember NOT ; after the path. Silly i know.Griseous
"Also, remember NOT ; after the path." Thank you for commenting.Objection
D
79
  1. In Eclipse click RunExternal ToolsExternal Tools Configurations.
  2. Click the JRE tab.
  3. Click the Installed JREs... button.
  4. Click the Add button.
    (Select Standard VM, where applicable.)
  5. Click the Directory button.
  6. Browse to your JDK version (not JRE) of your installed Java
    (e.g. C:\Program Files\Java\jdk1.7.0_04).
  7. Click Finish and OK.
  8. Select the JDK at Separate JRE and click Close.
  9. Re-run your Ant script — have fun!

This worked in a particular scenario I encountered.

Dyal answered 3/7, 2012 at 12:1 Comment(0)
S
34

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.

Sigil answered 13/10, 2009 at 7:59 Comment(3)
After hours and hours, this was it. Make absolutely sure that JAVA_HOME is set to C:\Program Files\Java\jdk1.6.0_14 . NOT the jre directory, NOT the bin directoryAlameda
What Christiaan said was the correct way to do it. Also, remember NOT ; after the path. Silly i know.Griseous
"Also, remember NOT ; after the path." Thank you for commenting.Objection
C
21

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

Cyrene answered 1/6, 2012 at 0:0 Comment(2)
You probably have the JRE/bin in the PATH before the JDK/bin (the installer does this). since ant is a java program run via java.exe - if it picks up the JRE/bin/java.exe it will set JAVA_HOME accordingly to the JRE location. If the JDK/bin is in the path first it will pick up java.exe from JDK and set JAVA_HOME to JDK location. fork="yes" works since runs a new process, javac.exe is run, it's not in JRE/bin so skips along to JDK/bin and finds it there. Cheers - MarkReconcilable
this worked great without copying tools to another directoryTimpani
H
16

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 :}

Heartbreak answered 5/4, 2011 at 21:0 Comment(1)
I can't scream it loud enough. This is exactly what I did wrong. It kept autoguessing, trying to use a non-existent JRE when I had pointed it at the JDK. This was really frustrating. As @Heartbreak said, just remove the trailing bin/ from your JAVA_HOME, and it will work.Cognac
F
8

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
Flinger answered 1/1, 2013 at 20:0 Comment(0)
M
7

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.

Maribeth answered 9/10, 2012 at 8:22 Comment(0)
P
5

To solve this problem add tools.jar file in window->preferences->ant-> runtime .

Phoenix answered 25/8, 2011 at 9:40 Comment(2)
to classpath? and if so, global entries or the other?Anselmi
I am running a simple embedded-jetty application in Eclipse. I added the GLASSFISH JSP support but kept getting this error. I tried several of the solutions here. The only thing that worked for me was this suggestion ... adding the tools.jar from the JDK lib directory. Thank you.Duralumin
C
4

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.

Crypto answered 4/6, 2011 at 7:21 Comment(0)
O
3

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.

Overpowering answered 13/10, 2009 at 8:5 Comment(0)
K
3

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.

Kortneykoruna answered 11/7, 2011 at 19:40 Comment(1)
OMG... fighting with this error since an hour. Thanks Brandon, this really saved me.Naaman
M
2

If you have JAVA_HOME set but there's a typo in it, you will also see the bogus reference to a jre6 path.

Marisamariscal answered 31/3, 2011 at 16:26 Comment(0)
O
2

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"/>
Overhand answered 11/9, 2012 at 9:38 Comment(0)
B
2

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

Bellicose answered 25/1, 2013 at 22:6 Comment(0)
W
2

When everything else is correct try the following

  1. Goto Window -> Preferences -> Ant -> Runtime -> ClassPath
  2. Expand Global Entries and check jdk tools available here.
  3. Add the required tools.jar from a specific version of java such as "C:\Program Files\Java\jdk1.6.0_45\lib\tools.jar"
  4. 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.

Weaks answered 27/3, 2014 at 21:14 Comment(0)
M
1

Just set the JDK path from the below steps:

  1. Go to 'Run' ->External Tools -> External Tools Configuration

  2. -> Select 'JRE' Then from the DropDown list select the available JDK . ex. jdk1.8.0_25

Michaella answered 14/1, 2016 at 18:59 Comment(0)
B
0

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.

Blondy answered 4/6, 2012 at 15:19 Comment(0)
F
0

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)

Fallen answered 16/10, 2012 at 11:25 Comment(0)
A
0

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.

Abednego answered 25/4, 2013 at 11:25 Comment(0)
A
0

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.

Arterial answered 8/9, 2013 at 12:48 Comment(0)
B
0

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.

Boelter answered 17/12, 2013 at 21:7 Comment(0)
R
0

FYI, I am using Windows 7 and had to restart Windows in order for the new JAVA_HOME setting to take effect.

Ranson answered 17/12, 2013 at 22:9 Comment(0)
V
0

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

Velarde answered 6/1, 2014 at 16:30 Comment(0)
O
0

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.

Othilie answered 20/6, 2014 at 19:51 Comment(0)
T
0

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"/>
Tollgate answered 24/3, 2016 at 17:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.