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.
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.
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!
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 saw
java 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.