Unable to find a javac compiler

Sarin J S picture Sarin J S · Dec 6, 2011 · Viewed 94.5k times · Source

I am trying to package my web application into war file using Ant.

When I build, I am getting the following error:

C:\Documents and Settings\Administrator\workspace\Assignment7\build.xml:67: 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"

The following is my build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="Generate War" default="install" basedir=".">


    <property name="build.dir" value="build"/>
    <property name="dir.name" value="Assignment7"/>
    <property name="package.name" value="${dir.name}.war"/>

    <property name="content" value="webContent"/>
    <property name="web-inf" value="${content}\WEB-INF"/>
    <property name="meta-inf" value="${content}\META-INF"/>
    <property name="jsp.dir.name" value="${content}"/>

    <property name="lib" value="${web-inf}\lib"/>
    <property name="src" value="src"/>
    <property name="dest.dir" value="target"/>

    <!-- Setting path to the server webapp folder -->

    <property name="webapp.dir" value="C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps"/>

    <!--  Using temp folder for convinence -->

    <property name="temp.dir" value="temp"/>
    <property name="temp.dir.web-inf" value="${temp.dir}\WEB-INF"/>
    <property name="temp.dir.meta-inf" value="${temp.dir}\META-INF"/>
    <property name="temp.dir.lib" value="${temp.dir.web-inf}\lib"/>
    <property name="temp.dir.classes" value="${temp.dir.web-inf}\classes"/>
    <!--<property name="temp.dir.classes.dir" value="${temp.dir.classes}\**"/>-->

    <property name="package.file" value="${dest.dir}\${package.name}"/>

    <path id="build.class.path">
        <fileset dir="${lib}">
            <include name="**/*.jar"/>
        </fileset>
    </path>
    <path id="build.class.path1">
        <fileset dir="C:\program files\java\jre6\lib">
            <include name="**/*.jar"/>
        </fileset>

    </path>

    <target name="clean">
        <delete>
            <fileset dir="${dest.dir}" includes="**/*"/>
        </delete>
        <delete dir="${temp.dir}"/>
        <delete dir="${temp.dir.classes}"/>
        <delete dir="${temp.dir.web-inf}"/>
        <delete dir="${temp.dir.meta-inf}"/>
    </target>


    <target name="prepare" depends="clean">
        <mkdir dir="${dest.dir}"/>
        <mkdir dir="${temp.dir}"/>
        <mkdir dir="${temp.dir.web-inf}"/>
        <mkdir dir="${temp.dir.lib}"/>
        <mkdir dir="${temp.dir.classes}"/>
        <mkdir dir="${temp.dir.meta-inf}"/>
    </target>

    <target name="compile" depends="prepare">
        <echo>==="complie"===</echo>
        <javac srcdir="${src}" destdir="${temp.dir.classes}" debug="on">
            <classpath refid="build.class.path"></classpath> 
        </javac>
        <!--<copydir src="${build.dir}\classes" dest="${temp.dir.classes}"/>-->
    </target>

    <target name="package" depends="compile">
        <echo>"PACKAGING THE FILES"</echo>
            <copy file="${meta-inf}\MANIFEST.MF" tofile="${temp.dir.meta-inf}\MANIFEST.MF" overwrite="true"/>
            <copy file="${web-inf}\web.xml" tofile="${temp.dir.web-inf}\web.xml" overwrite="true"/>
            <copy file="${web-inf}\tiles.xml" tofile="${temp.dir.web-inf}\tiles.xml" overwrite="true"/>
            <copy todir="${temp.dir.classes}">
                <fileset dir="${src}">
                    <include  name="**/*.xml"/>
                </fileset>
            </copy>

            <war destfile="${package.file}" webxml="${temp.dir.web-inf}\web.xml" basedir="${temp.dir}">
                <fileset dir="${jsp.dir.name}"/>
                <lib dir="${lib}"></lib>
                <classes dir="${temp.dir.classes}"></classes>
            </war>
    </target>

    <target name="jsps">
            <copy todir="${webapp.dir}\${dir.name}">
                <fileset dir="${content}">
                    <include name="**/*.jsp"/>
                    <include name="**/*.html"/>
                    <include name="**/*.css"/>
                    <include name="**/*.gif"/>
                    <include name="**/*.jpg"/>
                    <include name="**/*.png"/>
                    <include name="**/*.js"/>
                </fileset>
            </copy>
    </target>

    <target name="install" depends="package">
            <copy file="${package.file}" todir="${webapp.dir}" overwrite="true"/>
    </target>

</project>

How can I compile a class file that does not have a main class?

How do I set the class path?

For reference, I am using Eclipse.

Answer

Rebse picture Rebse · Dec 6, 2011

All your ant stuff will work fine except the javac task which needs the tools.jar, located in the /lib directory from the JDK, JRE is not sufficient in that case. Therefore the hint from ant : "Unable to find a javac compiler;..."

When working with Eclipse the default setting points to your JRE installation.
So, one of your first steps after starting Eclipse for the first time should be :
Window > Preferences > Java > Installed JREs and change the settings from JRE to JDK.
Alternatively use :
Window > Preferences > Ant > Runtime > Classpath > Global Entries
and add the tools.jar from your JDK/lib folder