Bad version number in .class file
Posted by danielmeyer on January 8, 2009
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…
- Updated the
PATHto point to jdk1.6.0_11′s bin directory instead of 1.5.0_15′s - Closed my command prompt and opened a new one (to get the environment changes), ran
java -version, and this time sawjava version "1.6.0_11"
(as it should be.)
- 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.
Origen said
Thanks, this was useful.
Jonathan Branam said
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.
danielmeyer said
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!
Asmitha Santhiar said
Thanks a lot – this article helped troubleshoot my problem. Thanks also to Jonathan Branam for his tip.
tedde44 said
Thanks!!! “Bad version number in .class file errors” error message really disappeared!!!
danielmeyer said
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!
ChrisP said
This helped me out of a bind. Thank you for taking the time to write it down!
britt said
Thanks!
Nayana Ganiger said
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
Manuel said
Hello,
I am on a Mac, can you tell me how to “do” step number one.
devesh said
Thanks a bunch !! stumbled here looking for a same issue and problem solved cleanly. Superb article
sainath said
im facing the problem of bad version information in wtk please help me in resolving this problem
avik said
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 ?
avik said
kindly advice me of the above.
danielmeyer said
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
Sachin D. Patil said
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.