Friday, September 23, 2011

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

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

[at java.lang.ClassLoader.defineClass1(Native Method)]

is an error that you may face in running a compiled java class file.

So as you can see from the stack trace, a class has not been loaded. But which class?

The problem is this error message does not show the name of the failed .class, isn't it?

No, it is not. This error is caused when you compile a .java file with one version of JDK and running the .class file with a different version of JVM.

Confused? You may say; same version is not required to compile and run.

Yes, that is true. But you can not run .class files that are compiled with a newer version than the JVM.

Say;

javac (compile) - version: X
java  (run)     - version: Y

If X is newer than Y; then you may face this issue at runtime.


Reference : http://lkamal.blogspot.com/2008/04/javalangunsupportedclassversionerror.html

Here is the supporting example.

Set the path to 1.6 for compilation
C:\Users\Yogi\Desktop>set PATH="C:\Program Files\Java\jdk1.6.0_10\bin";%PATH%

C:\Users\Yogi\Desktop>javac test.java

Now run the class using 1.5 jre
C:\Users\Yogi\Desktop>set PATH="C:\Program Files\Java\jre1.5.0_06\bin";%PATH%

C:\Users\Yogi\Desktop>java test
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version n
umber 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)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)


2 comments:

  1. Javin, Thanks for the link.
    My post also says the same thing...

    But yeah, its good to have more information about a problem at the same place.

    ReplyDelete