Bad version number in .class file

Recently I’d started getting errors like this in Eclipse:


Got an exception - java.lang.UnsupportedClassVersionError: Bad version number in .class file

It’s .java files that get the error, on “Line 0”.

My (False) Suspicions

We recently switched from Java 5 to Java 6, and my JAVA_HOME environment variable was pointing to the Java 6 installation as it should.  I began to suspect that Something was causing the libraries my project depends on to be compiled under Java 5, causing the Bad Version Number issue.

A Hint

Then I read Joachim Sauer’s reply to a question on this topic, which included this quote:

Also check “java -version” [to see] if you’re indeed executing the Java version that you think you are.

I ran java -version, and to my surprise I saw:

java version "1.5.0_15"

The True Cause

Though I had updated JAVA_HOME to point to Java 6, I had not updated the system PATH, which still pointed to Java 5.  So the issue was the opposite of what I thought: The libraries my project depended on were being (properly) compiled for Java 6, but I was accidentally compiling my project for Java 5!

Fixing it

I mainly needed to update the PATH variable, but there are measures you have to take to get the changes to propagate everywhere.

Step by step, then, I…

  1. Updated the PATH to point to jdk1.6.0_11’s bin directory instead of  1.5.0_15’s
  2. Closed my command prompt and opened a new one (to get the environment changes), ran java -version, and this time saw
    java version "1.6.0_11"

    (as it should be.)

  3. Closed and reopened Eclipse, went to Project -> Clean… and selected Clean all projects

When the workspace refreshed, all the Bad version number in .class file errors disappeared!

Update 1/15/2009: Another thing to check is that in Eclipse Window -> Preferences -> Java -> Installed JREs, the Java 6 JRE’s entry is there and that its checkbox is checked.

Advertisements

  1. #1 by Origen on January 8, 2009 - 10:32 am

    Thanks, this was useful.

  2. #2 by Jonathan Branam on January 8, 2009 - 10:48 am

    I find the best practice (can’t tell if you did this or not) is to use JAVA_HOME in the definition of PATH. So, PATH should be set to JAVA_HOME/bin, not to jdk_whatever/bin. Also it’s extremely hard to edit very long PATH definitions in Windows. I tend to copy the text out into Notepad, fix it, and paste it back in to avoid errors.

  3. #3 by danielmeyer on January 8, 2009 - 11:15 am

    Jonathan,
    Using JAVA_HOME in the definition of PATH – what a great idea! Remove duplication – of course! I guess I wasn’t in programming mode when I introduced that duplication. I’ve duly refactored my envionment variables. :) As so often happens, it seems obvious in retrospect!

  4. #4 by Asmitha Santhiar on May 8, 2009 - 2:20 am

    Thanks a lot – this article helped troubleshoot my problem. Thanks also to Jonathan Branam for his tip.

  5. #5 by tedde44 on May 15, 2010 - 2:45 am

    Thanks!!! “Bad version number in .class file errors” error message really disappeared!!!

  6. #6 by danielmeyer on May 15, 2010 - 6:39 am

    Thanks for leaving a comment, Tedde44 (and the others). I never knew till I started blogging how helpful it is to a blogger to get feedback on what helped someone. Glad to have been a help!

  7. #7 by ChrisP on October 14, 2010 - 4:00 pm

    This helped me out of a bind. Thank you for taking the time to write it down!

  8. #8 by britt on October 22, 2010 - 11:20 am

    Thanks!

  9. #9 by Nayana Ganiger on December 27, 2010 - 1:48 am

    hey, java -version point to 1.5 and eclipse JRE also set to 1.5 and environment variable path point to Path=%JAVA_HOME%\bin , i still find the below exception. Pls help.
    Exception:java.lang.UnsupportedClassVersionError: Bad version number in .class file

  10. #10 by Manuel on May 25, 2011 - 4:25 pm

    Hello,

    I am on a Mac, can you tell me how to “do” step number one.

  11. #11 by devesh on June 16, 2011 - 12:07 pm

    Thanks a bunch !! stumbled here looking for a same issue and problem solved cleanly. Superb article

  12. #12 by sainath on December 11, 2011 - 10:30 pm

    im facing the problem of bad version information in wtk please help me in resolving this problem

  13. #13 by avik on February 24, 2012 - 2:14 pm

    HI

    I created one jar at the following environment
    —————–
    Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02, mixed mode)
    —————-

    and running the jar at the following environment

    —————–
    jvm=w64_150
    Java=jdk1.6.0_26

    ——————-

    and I am getting “bad version # at .class” error, what change i need to do now ?

  14. #14 by avik on February 24, 2012 - 2:15 pm

    kindly advice me of the above.

  15. #15 by danielmeyer on February 24, 2012 - 2:56 pm

    Avik, I am not working in a Java environment at this time and I will not be able to help you further. I do hope you are able to find the answer.

    Daniel

  16. #16 by Sachin D. Patil on April 6, 2012 - 11:14 am

    Avik,
    If you have created jar file from Eclipse then check following settings:
    Right click on Project to Open the Properties dialog
    Go to “Java compiler”
    Set “Compiler compilance level” to 1.6 if you are using java 1.6 (This is the reason of your problem!)
    Apply the changes and close the Properties dialog.
    Then Export the jar file.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s