UnsupportedClassVersionError when running Ant from eclipse
Asked Answered
R

3

5

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!

Rentschler answered 29/1, 2013 at 10:54 Comment(0)
P
11

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.

Persistence answered 29/1, 2013 at 10:57 Comment(7)
The default java on my machine is 7, also the eclipse is using java 7, though the compliance level is set to 6 (changing it to 7 doesn't affect the error). I also have tools.jar under java 7.Rentschler
@NitzanTomer does running Ant in a separate process help?Persistence
What do you mean "separate process"? When I run in on command line (that's a separate process) it works fine, but if you mean do somehow run in in a separate process within eclipse, then I'm not sure what you mean.Rentschler
@NitzanTomer in the Ant builder settings on the JRE tab, it defaults to "Run in the same JRE as the workspace" but you can select "separate JRE" instead.Persistence
Oh, yeah, I have "Always run new Ant configurations in the same JRE as the workspace" box checked, is that what you mean?Rentschler
Also, the eclipse is running on java 7 and the project is set to use the default settings, so it too uses java 7.Rentschler
Ok, my bad. In the JRE tab of the any builder it was set to use different JRE. I only got to this window when going to External Tools ConfigurationRentschler
A
1

Your Eclipse ant task is trying to run the Java7 JDK compiler using a pre-Java7 JDK/JRE. Make sure you use a Java7 JRE to run Eclipse.

Afford answered 29/1, 2013 at 11:0 Comment(7)
My default java is 7, I assume that when I run eclipse it uses the default version, isn't that the case?Rentschler
Yes, I would expect so. Still, try Help->About Eclipse->Installation Details and look for java.version=... and/or java.runtime.version=...Afford
Yes, it runs as expected, it says: java.version=1.7.0_11Rentschler
Strange. I gather from the other answers/comments that your Ant build also has a JRE7 selected on the JRE tab? Have you tried fiddling with that?Afford
I'm not sure what you mean, fiddling with what exactly?Rentschler
Sorry... Trying out the other options.Afford
HA! ok, I missed something. In the External Tools Configuration there's the JRE tab which you and Ian talked about, I wasn't aware of this. Now that I found it, the error is gone. I'll accept Ian's answer since this part was mentioned in his original answer and it's not int he comments, which will make it easier for other people who stumble upon this question. Thanks a lot for your help!Rentschler
R
0

generally if you compile Java program in Java 1.7 it will create class file with major version 51.0 and if you run that on lower java version you will get

 java.lang.unsupportedclassversionerror unsupported major.minor version 51.0

check this post for more details on the same

Rociorock answered 29/1, 2013 at 10:58 Comment(1)
In this case the class whose version is unsupported is com.sun.tools.javac.Main - the Java compiler which lives in the JDK's tools.jar.Persistence

© 2022 - 2024 — McMap. All rights reserved.