Why is Ant giving me a Unsupported major.minor ver

2019-01-06 18:16发布

问题:

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.

Please help me resolve it.

回答1:

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\



回答2:

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



回答3:

  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:)



回答4:

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)



回答5:

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'.



回答6:

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.



回答7:

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.



回答8:

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.



回答9:

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.



回答10:

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.



回答11:

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.



回答12:

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



标签: java eclipse ant