2

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:

  1. I manually deleted the .jar file and did a clean on the Xerces-For-Android project to ensure no old binaries were laying around.
  2. I also did a clean on my sandbox project for testing the use of the jar file. Same issue.
  3. After the uninstalls of Java 7, I did a reinstall of java 6u45. Still no luck with getting past the 51.0 reference.
  4. As per Joop Eggen's request, I verified the compiler settings in eclipse:

enter image description here

enter image description here

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.

Community
  • 1
  • 1
James Oravec
  • 19,579
  • 27
  • 94
  • 160
  • possible duplicate of [Unsupported major.minor version 51.0](http://stackoverflow.com/questions/10382929/unsupported-major-minor-version-51-0) – DwB Sep 18 '13 at 17:06
  • summary: you can not run classes compiled for java 7 on a java 6 JVM. the classes in question are the classes inside the jar. – DwB Sep 18 '13 at 17:07
  • I'm compiling the jar and targeting version 6, as the post states. – James Oravec Sep 18 '13 at 17:19

3 Answers3

2

Make sure you do a full clean of everything before you do the build. The major.Minor thing happens for classes in the Jar, not necessarily for the jar itself.

Go through and remove any .class files you can find, and recompile from scratch.

rolfl
  • 17,539
  • 7
  • 42
  • 76
1

Disable automatic compilation in eclipse, or better run ant from the command line. And do <javac verbose="true" ...>. Check whether there is a JAVA_HOME set and the Ant installation.

Javac also has compiler and source attributes.

Maybe be paranoid and inspect the .class files, for code see dumpJavaClassVersion.

Community
  • 1
  • 1
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Your answer lead me to the core problem, as with the verbose command, it was clear that the ant clean task was not running, which cleaned the files in a different location than eclipse's clean. Thanks, this was driving me crazy. – James Oravec Sep 18 '13 at 17:41
0

If you are using maven to build your project, see below. Ensure that you specify source and target accordingly. I had this error when I compiled in 1.7 (kept both 1.7) but found that on amazon beanstalk, it was 1.6

<build>
<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <!-- http://maven.apache.org/plugins/maven-compiler-plugin/ -->
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
</plugins>
</build>
Deepti Kohli
  • 1,859
  • 1
  • 14
  • 6