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.


16 thoughts on “Bad version number in .class file

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

  2. 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!

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

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

  5. 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!

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

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

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

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



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

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


  11. 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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.