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:UsersYogiDesktop>set PATH=”C:Program FilesJavajdk1.6.0_10bin”;%PATH%

C:UsersYogiDesktop>javac test.java

Now run the class using 1.5 jre

C:UsersYogiDesktop>set PATH=”C:Program FilesJavajre1.5.0_06bin”;%PATH%

C:UsersYogiDesktop>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)

0 Replies to “java.lang.UnsupportedClassVersionError: Bad version number in .class file”

Leave a Reply