Java – Run ant script from command,error happened:Class not found: org.eclipse.jdt.core.JDTCompilerAdapter

anteclipse-jdtjava

In my java web project,there are code like <T> , in ant script, javac use JDK to compile java code, and it can't compile success.

Later,I know it must use eclipse JDT to compile.

And, in eclipse, ant script can run success.when run like this:

Right key click build.xml —> Run —> Run as —> External Tools Configurations,click JRE,select "Run in the same JRE as the workspace".

After that, ant can run successful, in eclipse.

But, I want to write a .bat and .sh file to call ant script, to compile,war,deploy and start Tomcat.
So, ant should run from command. I tried more, error happend always:
Class not found: org.eclipse.jdt.core.JDTCompilerAdapter

PS, I have copy jar files about JDT in eclipse plugin to ant_home/lib directory.

Wish your response. Thanks in advance!

build.xml

`

<tstamp>
    <format property="build.time" pattern="yyyy-MM-dd" />
</tstamp>

<path id="project.classpath">
    <fileset dir="${lib.dir}">
        <include name="**/*.jar" />
    </fileset>
    <fileset dir="${catalina.home}/lib">
        <include name="*.jar" />
    </fileset>
    <fileset dir="${ant.dir}">
        <include name="**/*.jar" />
    </fileset>
</path>

<target name="clear">
    <delete dir="${build.dir}" />
    <delete dir="${dist.dir}" />
    <delete file="${catalina.home}/webapps/${webapp.name}.war" />
    <delete dir="${catalina.home}/webapps/${webapp.name}" />
</target>

<target name="init" depends="clear">
    <mkdir dir="${build.dir}/classes" />
    <mkdir dir="${dist.dir}" />
</target>

<target name="compile" depends="init">
    <echo message="begin compile..." />
    <javac srcdir="${src.dir}" destdir="${build.dir}/classes" 
        includeantruntime="false" nowarn="on" 
        source="1.6" target="1.6" deprecation="true" debug="true" 
        encoding="UTF-8" classpathref="project.classpath">
        <compilerarg line="-Xlint:unchecked" />
    </javac>
    <copy todir="${build.dir}">
        <fileset dir="${src.dir}">
            <include name="**/*.xml" />
            <include name="**/*.properties" />
            <include name="**/*.sql" />
        </fileset>
        <fileset dir="${config.dir}">
            <include name="**/*.xml" />
            <include name="**/*.properties" />
            <include name="**/*.sql" />
        </fileset>
    </copy>
    <echo message="end compile..." />
</target>

<target name="war" depends="compile">
    <echo message="begin war..." />
    <war destfile="${dist.dir}/${webapp.name}.war" basedir="${webRoot.dir}" 
        webxml="${webRoot.dir}/WEB-INF/web.xml">
        <lib dir="${lib.dir}" />
        <classes dir="${build.dir}/classes" />
        <fileset dir="${webRoot.dir}">
            <include name="***.*" />
        </fileset>
    </war>
    <echo message="end war..." />
</target>

<target name="deploy" depends="war">
    <echo message="begin deploy..." />
    <copy file="${dist.dir}/${webapp.name}.war"    todir="${catalina.home}/webapps" />
    <echo message="end deploy..." />
</target>

</project>

`

Best Answer

Don't use the ant from eclipse IDE for usage from command line.

Download ant separately and extract it somewhere like - C:\apache\ant - for windows, and put its bin directory in your PATH. It'll come with some jars that will need to be added to your CLASSPATH too.

For Mac OSX 'sudo port install ant" takes care of everything.