UnsupportedClassVersionError when running Ant from eclipse

Nitzan Tomer picture Nitzan Tomer · Jan 29, 2013 · Viewed 11.1k times · Source

I'm trying to run an ant script from eclipse but it fails with the following message:

BUILD FAILED
project_path/build.xml:5:
java.lang.UnsupportedClassVersionError: com/sun/tools/javac/Main :
Unsupported major.minor version 51.0

However, when I run this from command line (./ant) it runs as expected.
Environment info:

  • OS: Linux Mint 64
  • Eclipse: Juno (4.2.1)
  • JDK: Oracle 1.7.0_11
  • Ant (command line): 1.8.2

I've also set the "Ant Home" (within eclipse settings) to be the same as the one I use from command line.

I've searched a lot on this issue but I can't seem to find anything that helped, I can try to uninstall all java (jdk/jre) I currently have installed, and then reinstall java 7, but I prefer not to take this route.

Any ideas? Thanks!

Answer

Ian Roberts picture Ian Roberts · Jan 29, 2013

UnsupportedClassVersionError: com/sun/tools/javac/Main : Unsupported major.minor version 51.0

This suggests there's some mix up between versions - Ant is trying to run the Java 7 javac on a Java 6 JVM. Have a look at your project properties, builders, Ant Builder, and check the classpath section. On my Eclipse with a newly-created Ant Builder I have the tools.jar from my 1.7 JDK listed under user entries -> additional tasks and support. If you have the same, but your Eclipse is itself running on Java 6 then it won't work. You can find out which Java Eclipse itself is running on via the "about Eclipse" box under installation details -> configuration. This may not be the same as the version of Java that your project is configured to use.

Another thing that might fix it is to go to the JRE tab in the ant builder settings and tell it to run in a separate JRE rather than running within the Eclipse process.