Friday, December 30, 2011

Error java.lang.UnsupportedClassVersionError: Bad version number in .class files Cause and Solution

How to fix Bad version number in .class file

"java.lang.UnsupportedClassVersionError: Bad version number in .class file" is common error in Java programming language which comes when you try to run a Java class file. In our last article we discussed that how to resolve Java.lang.UnSupportedClassVersionError and found that it comes when major and minor version of class is not supported by Java virtual machine or JRE running the program. Though "java.lang.UnsupportedClassVersionError: Bad version number in .class file" is little different than that in its manifestation and Cause. UnsupportedClassVersionError is not as difficult as Java.lang.OutOfMemoryError  and neither its solution is too complex but what is hard is thinking in right direction because cause of different types of UnsupportedClassVersionError is different.

Cause of java.lang.UnsupportedClassVersionError: Bad version number in .class file

java.lang.UnsupportedClassVersionError: Bad version number in .class file"java.lang.UnsupportedClassVersionError: Bad version number in .class file" comes when you compile a Java class in higher version of Java Compiler and run it on lower version of Java virtual machine or JRE.

Example of java.lang.UnsupportedClassVersionError: Bad version number in .class file

To understand this UnsupportedClassVersionError better let's reproduce it via a simple example in Java:

1) Create Loan.java and compile it with JDK 1.6

2) Run Loan.class with JRE 1.5.

C:\Program Files\Java\jre1.5.0_06\bin>java -version
java version "1.5.0_06"

C:\Program Files\Java\jre1.5.0_06\bin>java Loan
java.lang.UnsupportedClassVersionError: Bad version number in .class file
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)


Solution:
Now you know that your source is compiled for higher version of JRE or Java runtime, if it doesn't work in JDK 1.5 than try to run on JDK 1.6 and you will be able to remove "Bad version number in .class file"

Java Tutorial you may like:

No comments :

Post a Comment