I'm compiling a jar file targeted for java version 6, however I'm getting an error related to Unsupported major.minor version 51.0
. After reading several posts, such as How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version I was not able to solve my issue and took extreme steps to try to isolate the issue, as described below.
I created a java sandbox project whose purpose is to test the Xerces-For-Android jar that I'm making from source code and targeting to version 6. If I put the source code directly into the project, I'm able to compile and run my test program successfully. However if I make a jar file, then add that into my build path I get the Unsupported major.minor version 51.0
.
Reading several posts, the Unsupported major.minor version 51.0
seems like it is an issue based on compilation of different java versions. The version 51.0 looks to be java 7. To simply my testing, I uninstalled all Java 7 installs from my machine, ensured that my eclipse projects point to the same Java 6 JRE, and that my JAVA_HOME is set to Java 6u45. I also restart eclipse to make sure my changes were in place.
I'm using Ant
to create the jar file. The code is very simple and I even specify the javac target to be 1.6.
<project name="XercesForAndroid" default="dist" basedir=".">
<description>
Builds jar for Xerces-For-Android
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init" description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac target="1.6" srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the .jar file -->
<jar jarfile="${dist}/lib/Xerces-For-Android.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
After I run the ant script, I inspect the jar with 7-zip and find the manifest shows 6u45 was used to make it.
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_45-b06 (Sun Microsystems Inc.)
Any ideas on what I could be missing? I don't see how Java 7 could be referenced any more, but its seems like it is based on the 51.0 reference...
A couple other side notes:
- I manually deleted the .jar file and did a clean on the Xerces-For-Android project to ensure no old binaries were laying around.
- I also did a clean on my sandbox project for testing the use of the jar file. Same issue.
- After the uninstalls of Java 7, I did a reinstall of java 6u45. Still no luck with getting past the 51.0 reference.
- As per Joop Eggen's request, I verified the compiler settings in eclipse:
The solution:
As per Joop Eggen's request, I ran the ant script in verbose mode (ant -verbose > build.log
)... This gave me more information to identify the problem, which was the fact that the "clean" task of ant
was not running, which is different from the "project clean" in eclipse. The manifest file in the jar was being updated, but the class files were not being recreated as they showed as "up to date". After adding the clean task as a dependency to the compile task, everything worked as expected. Leaving this post in the off chance someone has a similar issue.