3

Possible Duplicate:
ant error JAVA_HOME does not point to SDK

I'm getting the following error:

BUILD FAILED
C:\Users\myname\Documents\CMSC\Proj1\build.xml:22: 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 (x86)\Java\jre7"

Total time: 1 second

My build.xml file contains the following code:

<project name="Project1" default="compile" basedir=".">

  <description>
    Build file for Project1
  </description>

  <!-- global properties for this build file -->
  <property name="source.dir" location="src"/>
  <property name="build.dir" location="bin"/>
  <property name="doc.dir" location="doc"/>
  <property name="main.class" value="proj1.Project1"/>

  <!-- set up some directories used by this project -->
  <target name="init" description="setup project directories">
    <mkdir dir="${build.dir}"/>
    <mkdir dir="${doc.dir}"/>
  </target>

  <!-- Compile the java code in ${src.dir} into ${build.dir} -->
  <target name="compile" depends="init" description="compile java sources">
    <javac srcdir="${source.dir}" destdir="${build.dir}"/>
  </target>

  <!-- execute the program with the fully qualified name in ${build.dir} -->
  <target name="run" description="run the project">
    <java dir="${build.dir}" classname="${main.class}" fork="yes">
        <arg line="${args}"/>
    </java>
  </target>

  <!-- Delete the build & doc directories and Emacs backup (*~) files -->
  <target name="clean" description="tidy up the workspace">
    <delete dir="${build.dir}"/>
    <delete dir="${doc.dir}"/>
    <delete>
      <fileset defaultexcludes="no" dir="${source.dir}" includes="**/*~"/>
    </delete>
  </target>

  <!-- Generate javadocs for current project into ${doc.dir} -->
  <target name="doc" depends="init" description="generate documentation">
    <javadoc sourcepath="${source.dir}" destdir="${doc.dir}"/>
  </target>

</project>

What am I doing wrong here? It appears that it's trying to find a javac file in my src folder but I don't know how to fix that.

Community
  • 1
  • 1
Haque1
  • 575
  • 1
  • 11
  • 21
  • 1
    @martinclayton Not really a duplicate as the guy is running the ant script using Eclipse. The environment configuration is slightly different to the configuration while running it from a command line. – Jagger Oct 09 '12 at 19:46
  • Jre and jdk are different things, you know. – Aleksandr M Oct 09 '12 at 19:06

4 Answers4

4

The JAVA_HOME must be pointed to JDK and not JRE.

To test it, before executing ant type set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.7.0_07 or whatever version you have.

Jagger
  • 10,350
  • 9
  • 51
  • 93
  • 1
    I'm running this from inside eclipse. To change it to the location of the JDK, do I have to modify eclipse's settings or the build file? – Haque1 Oct 09 '12 at 19:15
  • It's more of a shell setting. But Eclipse can invoke shells. There is a section for installed JDKs and JRE's. You need to be using the JDK. It has the binaries for things like, javac in it. – Greg Giacovelli Oct 09 '12 at 19:17
  • 2
    Try adding your `JDK` to Eclipse by using `Window`->`Preferences`->`Java`->`Installed JREs`. Click the button `Add...` and then set the JDK to the default JRE. – Jagger Oct 09 '12 at 19:19
4

Download the latest JDK here. Install it and notice where it goes. For example, the 32-bit JDK should install to C:\Program Files (x86)\Java\jdkSOMETHING.

In Eclipse, go to Window > Preferences > Java > Installed JREs and click Add > Standard JVM (then Next). Browse to the directory noted above for the JDK and then hit Finish. To check that your project is using this JDK, right-click the project and go to Properties. From there, look for Java Build Path. Under the Libraries tab you should see a JRE System Library. If it's not the JDK you just added, click the entry and hit the Edit button and change it.

Now run your Ant build.

davidfmatheson
  • 3,539
  • 19
  • 27
0

Looking at your error, looks like its Windows environment and you are running ant through command prompt.

Follow Right Click(My Computer) -> properties -> Advanced 
-> Environment Variables -> System Variables -> New
Add  Variable Name = JAVA_HOME
     Variable Value = Path to your Base folder of Java 
                      e.g. C:\Program Files\Java\jdkxxx

Restart the command prompt, type java -version. If it prints your java version correctly, you should be all set.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
-1

it is saying

"Perhaps JAVA_HOME does not point to the JDK."

Perhaps you should check that :D

sheidaei
  • 9,842
  • 20
  • 63
  • 86
  • I'm new to this and I'm using eclipse. Should I change eclipse's settings or the build file? – Haque1 Oct 09 '12 at 19:17
  • 1
    try searching for something like this "how to run ant from eclipse" in Google and follow the instructions. – sheidaei Oct 09 '12 at 19:20