45

I'm running this target in Ant.

<target name="compile" depends="init"
    description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
</target>

It's giving me this error.

C:\Users\..\workspace\..\build.xml:21: java.lang.UnsupportedClassVersionError: 
    com/sun/tools/javac/Main : Unsupported major.minor version 51.0

Here are the versions:

Apache Ant(TM) version 1.8.2

JDK version is JDK 1.7.0

Here's my Compiler settings in Eclipse.

enter image description here

Please help me resolve it.

Community
  • 1
  • 1
coder
  • 6,111
  • 19
  • 54
  • 73
  • What Java compiler is selected for the project? Have you rebuilt recently? – trashgod Aug 16 '11 at 04:03
  • I'm not sure what compiler is selected for the project. I'm using Eclipse and the Compiler Compliance Level is 1.6. I'm not sure if that answers the question. – coder Aug 16 '11 at 04:46
  • IIRC, the Java compiler setting is in the project's properties. – trashgod Aug 16 '11 at 04:48
  • @trashgod Does the screen shot I attached tell you anything? – coder Aug 16 '11 at 04:55
  • Check the project level settings, too, but I'm guessing you are using a 1.7 feature while asking the compiler to generate 1.6 compatible classes. – trashgod Aug 16 '11 at 05:16

12 Answers12

36

Check if tools.jar used during compilation complies with the JRE set. If you are on eclipse, verify the versions as:
Goto Window->Preferences
- Check Ant Runtime by selecting Runtime under Ant in the left pane. Check the Global Entries under classpath tab. You can see the version in folder path.
- On the left pane check installed JREs under Java. Check the version.
I had also got the same problem when my selected JRE was 1.6.0_24 and tools.jar in Ant runtime was coming from jdk 1.7 If both of them are from different versions- 1.6/1.7, correct them by correcting their path. Typically, tools.jar can be found at :C:\Program Files\Java\jdk1.x.x\lib. Likewise if required add the JRE by pointing it to C:\Program Files\Java\jdk1.x.x\

abksrv
  • 1,347
  • 1
  • 15
  • 34
27

The runtime jre was set to jre 6 instead of jre 7 in the build configuration window.

coder
  • 6,111
  • 19
  • 54
  • 73
  • 4
    Btw since it's a problem with v6 and v7 misconfigurations, Why is the error message reporting the unsupported version number is 5 / 1.0 ? – Pacerier May 09 '12 at 10:22
16
  1. Check whether u have jdk installed in the path "C:\Program Files\Java" If not Install the JDK in your machine

  2. In Eclipse, right click on "build.xml" then select Run As > External Tools Configuration

  3. Click on "JRE" tab then click on "Installed JREs" > "ADD" > "Standard VM" > Click "Next

  4. Select the Directory "C:\Program Files\Java\jdk1.7.x_xx" and the directory will be added to the "installed jres"

  5. Select the new JDK directory and Click "OK"

  6. Click on "Seperate JRE" dropdown and select the JDK version "jdk1.7.x_xx" and click on "Run"

This would help:)

SuperSaiyan
  • 159
  • 1
  • 4
9

You would need to say which version of Ant and which JVM version.

You can run ant -v to see which settings Ant is using as per the doc

Ant 1.8* requires JDK 1.4 or higher.

The 'Unsupported major.minor version 51.0' means somewhere code was compiled for a version of the JDK, and that you are trying to run those classes under an older version of the JDK. (see here)

Nicolas Modrzyk
  • 13,961
  • 2
  • 36
  • 40
  • This makes sense, and I had looked at the link that you included before I created this question. I posted the question because, it doesn't seem that I 'should' have this issue. I'm using all of the newest releases of Ant/JDK/JRE/Spring/Hibernate and have even tried to update to the newest version of Eclipse to ensure that was not the cause, but still got the error. I'm looking for other answers. – coder Aug 16 '11 at 14:24
  • I was not using eclipse and get the same error, this is related to **java version** and the version used for compile ant. You may found more info here: http://hybrisdude.blogspot.com/2015/09/hybris-setup-issue-unsupported.html – TulioPa May 04 '16 at 20:19
4

One possible cause of this is an incorrect JRE selected in the Ant build options. After right-clicking the build.xml and choosing 'Run As...' and then 'Ant Build...', make sure the correct JRE is chosen under the JRE Tab of the configuration options dialogue box. You will see a 'Separate JRE' option; make sure the appropriate jdk is chosen from the drop down before clicking 'Run'.

THX1137
  • 903
  • 6
  • 15
  • This worked out of all the responses I have checked. This is the right answer in my opinion. – Axwack Sep 10 '14 at 12:36
2

I run into the same problem. Then I went into Run as -> Ant build...->jre. I found the jre used is separate JRE which is the default eclipse JRE(1.6). Then I went to the perferences ->installed JREs . And change the location of the default eclipse JRE to my jdk(1.7).

The problem is resolved.

2

Download the JDK version of the JRE to the installed JRE's and use that instead.
In Eclipse Indigo, if you check the classpath tab on the run configuration for ant, you will see that it defaults to adding the tools.jar from the system. So if you launch Eclipse using Java7 and run an ant build using a separate JRE6 it generates an UnsupportedClassVersionError. When I added the JDK version Eclipse picked up the tools.jar from the JDK and my ant task ran successfully.

GMD
  • 698
  • 5
  • 12
Aberdeen61
  • 21
  • 1
1

I'd like to add my results here to help others, as while relevant, none of these solved my problem.
I had the unique situation where I went from workshop for weblogic 9.2 to Helios with JDK6. However our security experts pushed us on to JDK7, which forced my hand to go to Indigo.

In the end I used sysinternals process explorer to find if anything was currently using the JDK6 folder that still existed on my system. Sure enough it was Eclipse. I opened up eclipse.ini and the second line was referencing the JDK6, I replaced this with the JDK7 directory, started Eclipse, and the build was successful.

0

If you're getting this error because you're purposefully trying to build to Java 6, but you have Java 7 elsewhere in Eclipse, then it may be because you are referencing a Java 7 tools.jar in a Java 6 environment.

You'll need to install the JDK 6 (not JRE) and add the JRE 6 tools.jar as a User Entry in the Classpath of the build configuration, listed above the JRE 7 tools.jar.

jevon
  • 3,197
  • 3
  • 32
  • 40
0

In my case, the project was a Maven, I had JDK 1.8.0, Eclipse : Kepler, and I installed the M2Eclipse plugin from Eclipse Marketplace.

Changing compiler level didn't help.

Finally I used latest version of eclipse (Luna), compiler level 1.7, same M2Eclipse plugin and problem was solved.

Morix Dev
  • 2,700
  • 1
  • 28
  • 49
Itack
  • 222
  • 1
  • 3
  • 13
0

go to run option--->select external tool option ---->external tool configuration---> select ant build ----> select your build.xml --->there you will find JDK option--->select your required JRE.

0

Simply just check your run time by go to ant build configuration and change the jre against to jdk (if jdk 1.7 then jre should be 1.7) .

enter image description here