27

I am trying to build a project using Ant in eclipse. I right-clicked on build.xml > Run As > Ant Build. However, I am getting the following error:

BUILD FAILED
C:\Users\David\eclipse\test-project\build.xml:26: Class not found: javac1.8

and also a warning:

compile:
[javac] C:\Users\David\eclipse\test-project\build.xml:26: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

As I read in other posts that this might be due to having an ant version that is too old or not having set the environment variables correctly here is all the info:

C:\>java -version
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

C:\>ant -version
Apache Ant(TM) version 1.9.3 compiled on December 23 2013

C:\>echo %JAVA_HOME%
C:\Program Files\Java\jdk1.8.0_05

C:\>echo %JRE_HOME%
C:\Program Files\Java\jre8

EDIT: Here is the whole build.xml, line 26 is the javac tag:

<?xml version="1.0"?>
<project name="test-project" default="main" basedir=".">
  <!-- Sets variables which can later be used. -->
  <!-- The value of a property is accessed via ${} -->
  <property name="src.dir" location="src" />
  <property name="build.dir" location="bin" />
  <property name="dist.dir" location="dist" />
  <property name="docs.dir" location="docs" />

  <!-- Deletes the existing build, docs and dist directory-->
  <target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${docs.dir}" />
    <delete dir="${dist.dir}" />
  </target>

  <!-- Creates the  build, docs and dist directory-->
  <target name="makedir">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${docs.dir}" />
    <mkdir dir="${dist.dir}" />
  </target>

  <!-- Compiles the java code (including the usage of library for JUnit -->
  <target name="compile" depends="clean, makedir">
    <javac srcdir="${src.dir}" destdir="${build.dir}">
    </javac>
  </target>

  <!-- Creates Javadoc -->
  <target name="docs" depends="compile">
    <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
      <!-- Define which files / directory should get included, we include all -->
       <fileset dir="${src.dir}">
                <include name="**" />
           </fileset>
    </javadoc>
  </target>

  <!--Creates the deployable jar file  -->
  <target name="jar" depends="compile">
    <jar destfile="${dist.dir}\test-project1.jar" basedir="${build.dir}">
      <manifest>
        <attribute name="Main-Class" value="test.Main" />
      </manifest>
    </jar>
  </target>

  <target name="main" depends="compile, jar, docs">
    <description>Main target</description>
  </target>

</project> 
David_Z
  • 301
  • 1
  • 3
  • 8
  • Show us your build.xml and indicate which is line 26. – greg-449 May 03 '14 at 09:37
  • Ant uses a compiler adapter to invoke the Javac compiler. For modern builds, the adapter is determined based on the Java version, so using Java 1.8 will try to use adapter javac1.8 unless the **build.compiler** property or **compiler** attribute is defined. That adapter is only available in Ant version 1.8.3 or later. The **includeantruntime** warning tells the javac task whether Ant jar files should be in the path during compiles, default is true. It is recommended it be set to false so you are only picking up the environment you mean to pick up. – Jere May 03 '14 at 12:19
  • Ok, just saw you were using 1.9.3 so shouldn't be a problem. Can you run Ant with the -d flag and look to see what ant thinks the environment is? – Jere May 03 '14 at 12:31
  • Yes I'm using the newest ant version. I'll just use jdk 1.7 as I posted in the answer. – David_Z May 03 '14 at 12:35
  • Do you have `$JAVA_HOME` set? If so, does `$JAVA_HOME/javac -version` do anything? Just making sure you're hitting the JDK and not a JRE. A JRE won't have `javac` to execute, and will give you that error. – David W. Jul 09 '14 at 19:46
  • Can you run this build from the command line and not through Eclipse? That will help us figure out if the error is with Ant or Java itself, or the error is in Ecilpse's configuration. – David W. Jul 09 '14 at 19:49
  • Also take a look [at this issue](http://stackoverflow.com/a/22524712/368630) on Stackoverflow – David W. Jul 09 '14 at 19:50
  • Possible duplicate of [Eclipse and Ant: javac1.8 class not found](http://stackoverflow.com/questions/20702626/eclipse-and-ant-javac1-8-class-not-found) – jww Aug 15 '14 at 04:06
  • Please go through the answer at the below link http://stackoverflow.com/questions/20702626/javac1-8-class-not-found/33587921#33587921 – KulDeep Nov 07 '15 at 21:44

6 Answers6

33

The version of Ant bundled with your version of Eclipse is not compatible with Java 1.8.

Go to the Ant download page, and extract the latest version somewhere appropriate onto your filesystem.

In Eclipse, go to Window > Preferences > Ant > Runtime, click the Ant Home... button, and select the location that you extracted the newly downloaded Ant to.

jr.
  • 1,699
  • 14
  • 31
  • The latest version of Ant today is 1.9.6. However, it still doesn't work with java 1.8. – IgorGanapolsky Apr 06 '16 at 13:22
  • 3
    Ant 1.9.6 certainly supports Java 8. In fact, [Ant has had Java 8 support since 1.9.1](https://wiki.eclipse.org/Ant/Java8). If Ant 1.9.6 is not working for you, perhaps there is a different problem with your configuration. – jr. Apr 07 '16 at 00:08
7

To make it still more clear.

1>Set JAVA_HOME,JRE_HOME and Update Ant to 1.9

2>Over build.xml right click => run as (this takes into configuration)==>Now in this Edit Configuration and launch pop-up window

select Main tab then the third form field called "Arguments" add:

-Dbuild.compiler=javac1.7

3> In build.xml add includeantruntime="false"

<javac srcdir="${src}" destdir="${bin}" debug="true" encoding="ISO-8859-1" includeantruntime="false">
        <classpath refid="my.classpath"/>
</javac>

Edit configuration

It should compile without any message

vimal krishna
  • 2,886
  • 28
  • 22
3

Mr. studro is right. I just confirmed an example on Ubuntu 14.04

sudo apt-get install ant
ant -version
Apache Ant (TM) Version 1.9.3 compiled on April August 2014

works perfectly in eclipse, just follow the steps described by Mr studro to configure the 'Ant Home' in eclipse with "/usr/share/ant".
Regards, Stéphane.

Stéphane Millien
  • 3,238
  • 22
  • 36
1

I think, what you are seeing is Ant Bug 53347 (see https://issues.apache.org/bugzilla/show_bug.cgi?id=53347).

If so, try either pf the following workarounds:

Set the property "build.compiler" to a meaningful value like "javac1.7", or "javac1.3".

Set the "compiler" attribute of the "javac" element of your build script to either of the above values. For all possible values, and their meaning, see http://ant.apache.org/manual/Tasks/javac.html

user1774051
  • 843
  • 1
  • 6
  • 18
0

Make sure your source files are in "ProjectDirectory/src".

kasi
  • 955
  • 1
  • 12
  • 21
  • they are in "test-project/src". In subfolders though but that's not the issue. – David_Z May 03 '14 at 11:06
  • When you try `javac -version` ? – kasi May 03 '14 at 11:27
  • oh the jdk/bin path was not in the PATH variable, so javac -version didn't work. But I added it to the environment variable and then javac -version gives this: javac 1.8.0_05 However, building with ant still doesn't work. – David_Z May 03 '14 at 11:37
-1

I already did some extensive googling before asking this question but continuing that I found a solution here: This issue seems to only appear when using JDK 1.8 so using JDK 1.7 instead solves the problem. The following line needs to be added to eclipse.ini:

-vm "path-to-jdk-1.7\bin\javaw.exe"
David_Z
  • 301
  • 1
  • 3
  • 8