83

I have installed two jdks - jdk 1.5 and jdk 1.8. I have following Ant build.xml config file :

<target name="compileCustomSrc">
    <javac srcdir="src" destdir="build/classes" source="1.5" target="1.5" >
        <classpath>
            <fileset dir="C:/lib/">
                <include name="*.jar" />
            </fileset>
        </classpath>
    </javac>
</target>

Before installing jdk1.8, Ant invoked from Eclipse compiled all sources successful. But now, I have following error message:

Class not found: javac1.8

My JAVA_HOME and JRE_HOME:

JAVA_HOME = C:\Program Files (x86)\Java\jdk1.5.0_16
JRE_HOME = C:\Program Files (x86)\Java\jre1.5.0_16

and set in Eclipse JRE to 1.5. Does Java 1.8 set some configs during installation?

jww
  • 97,681
  • 90
  • 411
  • 885
kamilz
  • 833
  • 1
  • 6
  • 7

13 Answers13

176
Class not found: javac1.8

This error is known to happen with Apache Ant versions less than 1.9.0 - which aren't compatible with Java 8. If you're using a version of Ant < 1.9.0, you should update to a 1.9.x release.

Here is the related bug report: https://issues.apache.org/bugzilla/show_bug.cgi?id=53347

Btw, if you just want to compile with Java 8, but don't need to compile any actual Java 8 syntax, then you can pass the following argument to javac as a workaround, which would allow you to continue using an older version of Ant:

-Dbuild.compiler=javac1.7
Kristian Holdhus
  • 2,406
  • 1
  • 15
  • 7
  • 9
    I ran into this issue, and the given flag (`-Dbuild.compiler=javac1.7`) works when passed directly to `ant` as well. – cincodenada Apr 18 '14 at 05:33
  • 5
    I found the [code](http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/compilers/CompilerAdapterFactory.java?r1=1037667&r2=1345316&pathrev=1345316&diff_format=f). They're testing whether you're using Java 1.3 or below. Old, old code from over a decade ago. Because of this, Ant breaks each time Oracle comes out with a new 1.x version. Ant already requires 1.4 or higher. They should just remove this check from the code. – David W. Jul 09 '14 at 20:28
  • 3
    @ David W - why the hell didn't they add support for Java 1.9 and 2.0 so the changes work for the next 6 or 8 years. Java 9 will be out soon, and everything is going to break again.... – jww Aug 15 '14 at 04:42
  • Kristian answer is correct, we could add -Dbuild.compiler=javac1.7 when generating the ant-launch config in eclipse see – Shiva Achari May 11 '14 at 05:29
  • 2
    The bug was fixed in 2012 for Eclipse's Ant 1.9.0 but Ant in Eclipse Kepler (2014!) is still at 1.8.4 and so this problem still exists. Is this upgraded in Luna or are we still waiting? (I find Eclipse's Web site utterly useless for any kind of proper change tracking and knowing what versions of what plugins went into what releases; is this there anywhere?) – Stuart Rossiter Jan 14 '15 at 13:13
  • Thanks, just hit this with ant on MacOS 10.8 while testing for backwards compatibility for an app. Workaround was plenty to fire off the build with 1.7. – tresf Nov 15 '16 at 03:03
  • Note - You can get past this error by using ant 1.8.2, if you need to stick to 1.8.X versions for some reason. I tried it with 1.8.4, and it failed, but worked fine with 1.8.2 – Brad Parks Jun 19 '17 at 17:15
14

As a follow-on to Kristian Holdhus's answer (marked as correct), you can instead use the compiler attribute on the Ant javac task. Setting compiler="modern" should be both backward (as far back as 1.3) and forward compatible.

This avoids the need to set the Java System property named build.compiler. Only in the absence of explicitly setting the compiler attribute on the javac task, will Ant use the value of the build.compiler as a fallback.

Chuck Daniels
  • 312
  • 3
  • 6
  • I certainly upvoted Kristian Holdus' answer. However, I found this to be a better solution for my situation. By forcing the callers to add a -D param, we are making it difficult to ship the build.xml to external users. We also need to provide additional usage instructions, which are always lesser the better :) – Rakesh N Jul 04 '16 at 09:03
  • its working, showing notes when building `[javac] warning: C:\Program Files\Java\jre1.8.0_152\lib\rt.jar(java/net/ConnectException.class): major version 52 is newer than 51, the highest major version supported by this compiler. [javac] It is recommended that the compiler be upgraded.` etc – aswzen Feb 01 '18 at 03:35
7

I had this same issue. I had JAVA_HOME and JRE_HOME set correctly. I also had Ant > 1.9.

What I did to fix it is go into Eclipse to Window -> Preferences. Expand Ant and select Runtime. By default the Classpath tab should be selected and on the right hand side there is a button called "Ant Home". Click this button and double check it is pointed to your Ant folder.

Mike Kuenzi
  • 301
  • 5
  • 6
3
JAVA_HOME = C:\Program Files (x86)\Java\jdk1.5.0_16
JRE_HOME = C:\Program Files (x86)\Java\jre1.5.0_16

Your JAVA_HOME and JRE_HOME are set to the Java 1.5 installation hence 1.8 binaries (like javac) are not found. Set them to the Java 1.8 installation and you should be fine.

Toni Toni Chopper
  • 1,833
  • 2
  • 20
  • 29
  • But I don't want to use jdk1.8. Why Ant try to use 1.8 compiler with this settings? – kamilz Dec 20 '13 at 11:33
  • 1
    Run the Ant build with "Run As" > "Ant Build..." then open the "JRE" tab and select the "Runtime JRE" you want to use to run the script. – Toni Toni Chopper Dec 20 '13 at 11:36
  • The "javac" task can generate byte code for older vms, See the "target" attribute in the doco: https://ant.apache.org/manual/Tasks/javac.html Java 1.5 cannot generate bytecode for later VMs. – Mark O'Connor Dec 21 '13 at 12:22
3

I tried it with Eclise and got the same error Class not found: javac1.8

I verified the ANT version on my system

>ant -version

Apache Ant(TM) version 1.9.6 compiled on June 29 2015

When I tried to run the build.xml from the command prompt, it worked. Then I checked that in the Eclipse, it was referring to the older version of the ANT.

I simply edited the ANT HOME path in the Eclipse and it worked from here also. :)

Below is the image from where need to edit the ANT Path in the Eclipse.

  1. Right click on the build.xml file
  2. Run as
  3. External tool configurations
  4. Select the classpath tab

Now change the ANT Home path from here.

enter image description here

KulDeep
  • 441
  • 4
  • 8
  • What did you change the ANT Home path to? – IgorGanapolsky Apr 06 '16 at 13:30
  • 1
    Hi @IgorGanapolsky Earlier the Ant version path was for Ant 1.8 which I updated to the latest Ant version 1.9 as shown in the image above. And it worked. Hope I answered your question. – KulDeep Jun 26 '16 at 18:44
1

Update Ant to 1.9.x release as Java 8 in incompatible with Ant versions less tha 1.9

Neha Priya
  • 11
  • 1
1

I tried all of the other solutions, but none worked. I finally found an approach that worked for me in Eclipse, follow these steps:

  1. Right click on the ant task name, e.g. `myBuild.xml in the ant window.

  2. Choose Run As | External Tool Configurations.

  3. Then on the JRE tab, choose Separate JRE: Java JDK7 or whatever your java 7 is named.

  4. Hit Apply and then Run.

Your ant task will be executed, and will build correctly!

Federico Baù
  • 6,013
  • 5
  • 30
  • 38
manchaw
  • 11
  • 1
0

I tried all of the other solutions, but none worked. I finally found an approach that worked for me in Eclipse:

  • Right click on the ant task name, e.g. "compileCustomSrc" in the ant window.
  • Choose Run As | External Tool Configurations
  • Then on the JRE tab, choose Separate JRE: Java SE7 or whatever your java 7 is named, then hit Apply and then Run.

Your ant task will be executed, and will build correctly!

Brad Parks
  • 66,836
  • 64
  • 257
  • 336
0

Update Ant to 1.9.x release as Java 8 in incompatible with Ant versions less tha 1.9 or after the comand put -Dbuild.compiler=javac1.7, see the below:

ant release -Dbuild.compiler=javac1.7

linhadiretalipe
  • 907
  • 7
  • 8
0

you can download and unzip ANT 1.10 on your system and try to chnage the ANT home in eclipse for your project to 1.10. It should work, Read this for more details. http://devops-badal.blogspot.com/2017/02/class-not-found-javac18-issue-while.html

0

Sometimes the issue while building the war through ANT is also related to java version miss match in build.properties files as build.XML tries to use the java version mentioned in build.compiler property

check whether build.compiler property is same as intended java version.

SoSufi
  • 93
  • 1
  • 5
0

You can download a newer version of eclipse, which includes a newer version of Ant in the plugins directory. Then, copy the Ant jar into the older eclipse's plugin directory. Eclipse will automatically point to the newer version of Ant. This worked for older Kepler with newer Mars2 for me.

techron
  • 17
  • 3
0

I had the same problem. And I was able to solve it within the build.xml by setting a property.

<property "build.compiler=javac1.7">
Pavan Dittakavi
  • 3,013
  • 5
  • 27
  • 47