Friday, July 1, 2011

How to resolve java.lang.UnsupportedClassVersionError with example

java.lang.UnsupportedClassVersionError  is a quite common error after NoClassDefFoundError or ClassNotFoundException they all seems to related to class files but they all are different and there cause and resolution are different. In this java tutorial we will see what is UnsupportedClassVersionError in Java? Why UnsupportedClassVersionError comes in Java? What is class file format and version numbers associated with it and finally how to resolve UnsupportedClassVersionError in Java. 

This article is in continuation of debugging tutorials like How to remote debug Java program in Eclipse and 10 Java debugging tips in Eclipse. If you have not read those article you may find them useful.

 

How to resolve UnsupportedClassVersionError in Java

What is UnSupportedClassVersionError in Java?

UnSupportedClassVersionError, java.lang.UnsupportedClassVersionErrorJava.lang.UnsupportedClassVersionError is a subclass of java.lang.ClassFormatError. This is a kind of linking error which occurs during linking phase accordingly java.lang.ClassFormatError has also derived from java.lang.LinkageError. As the name suggests "UnSupportedClassVersionError" so it’s related to unsupported class version, now questions comes what is class version in Java? Well every source file is compiled into class file and each class file has two versions associated with it, major version and minor version. Version of class file is represented as major_version.minor_version. This version is used to determine format of class file in Java.
According to Java Virtual Machine specification, “A JVM implementation can support a class file format of version v if and only if v lies in some contiguous range Mi.0 v Mj.m. Only Sun can specify what range of versions a JVM implementation conforming to a certain release level of the Java platform may support.” For example: JDK 1.2 supports class file formats from version 45.0 to version 46.0 inclusive. So if a class file has version 48.0 it means that major version of class file is "48" and minor version is "0", which tells us that JDK 1.4 has been used to compile and generate that class file.



When UnSupportedClassVersionError in Java comes:

So now we got the theory behind class file format and major and minor version of class file in Java. Now a million dollar question is when UnSupportedClassVersionError in Java does occur?  precise answer of this is "When JVM tries to load a class and found that class file version is not supported it throws UnSupportedClassVersionError and it generally occurs if a higher JDK version  is used to compile the source file and  a lower JDK version is used to run the program. for example if you compile your java source file in JDK 1.5 and you will try to run it on JDK 1.4 you will get error "java.lang.UnsupportedClassVersionError: Bad version number in .class file [at java.lang.ClassLoader.defineClass1(Native Method)]".

But its important to note is that vice-versa is not true "you can compile your program in J2SE 1.4 and run on J2SE 1.5 and you will not get any UnSupportedClassVersionError". When a higher JDK is used for compilation it creates class file with higher version and when a lower JDK is used to run the program it found that higher version of class file not supported at JVM level and results in java.lang.UnsupportedClassVersionError.


How to fix UnSupportedClassVersionError

Now we know the root cause of UnSupportedClassVersionError that we are using a lower JVM for running the program. But major problem is that stack trace of UnSupportedClassVersionError will not tell you for which class it’s coming. So if you are using multiple third party jars in your application you find that it comes at a particular part when JVM tries to load a class from a particular jar. anyway we all know that latest version of JDK is 1.6 so maximum version of class file could be generated by JDK 6, so by using JDK 6 we can solve UnSupportedClassVersionError, but many times its not easy to just move to higher JDK version. So I would suggest:

1) Find out due to which jar or class file this UnSupportedClassVersionError is coming?
2) Try to compile source code of that jar with the JDK version you are using to run your program, if source is available.
3) If you don't have source try to find the compatible version of that library.
4) Increase the JRE version you are using to run your program.

You can go by any approach to resolve UnSupportedClassVersionError based upon your need. Generally a higher JVM version is ok and does not cause any problem unless the class file format is quite old and no more supported by Sun in higher JVMs. The best way to deal with UnSupportedClassVersionError in Java is to use same version or JDK and JRE for compiling and running your program.



Example of UnSupportedClassVersionError in Java

You can easily reproduce UnSupportedClassVersionError by using javac of higher JDK and "java" from lower Java version. Let’s see some of examples of UnSupportedClassVersionError in Java:

1) java.lang.UnsupportedClassVersionError: EquityTradingManager (Unsupported major.minor version 49.0)
      at java.lang.ClassLoader.defineClass0(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
==>Since we know that major version 49 is supported by JDK 1.5, so these will "java.lang.UnsupportedClassVersionError” will come if JVM used to run this program is lower than Java 1.5.

2) Java.lang.UnsupportedClassVersionError: Bad version number in .class file

3) java.lang.unsupportedclassversionerror unsupported classversion 50.0
==> Compile in JDK 1.6 and running on lower version than Java 6.

4) java.lang.unsupportedclassversionerror unsupported classversion 49.0
==> compiled in Java 5 and running on lower JVM than JDK 5.

5) java.lang.unsupportedclassversionerror bad version number in eclipse.
java.lang.unsupportedclassversionerror unsupported classversion 49.0 or 50.0
==> Most of us use eclipse for building and running project some of us also use ant for building project. In eclipse there is some setting related to java source version which if you got incorrect can result in "java.lang.unsupportedclassversionerror bad version number". so make sure you have correct configuration. For example if you compile with source compatible 1.6 you need JRE 6 to execute the program. To check the compiler setting in eclipse go to project  ==>Properties==>Java Compiler as shown in image





Important point about UnSupportedClassVersionError in Java:

1) If you encounter UnSupportedClassVersionError, check the JRE version you are using to run program and switch to higher version for quick solution.
2) java.lang.UnsupportedClassVersionError is derived from java.lang.LinkageError, so it will not be detected in compile time and it will only come on runtime, precisely when JVM tries to load a class.
3) Class file format which is identified using major version and minor version. Class file format is assigned when you compile source file and its depends on JDK version used to compile.
4) Its always best practice to use same version of java for compilation and execution to avoid any chance of UnSupportedClassVersionError.
5) UnSupportedClassVersionError is not related to java classpath , so don't confuse this with NoClassDefFoundError or ClassNotFoundException.

Major Class Versions of Various JDK

Following are the major version of class file format in standard JDK environment.

JDK 1.1 = 45
JDK 1.2 = 46
JDK 1.3 = 47
JDK 1.4 = 48
JDK 1.5 = 49
JDK 1.6 = 50

You can also get version of "javac" (used for compilation) and version of "java" (used for execution) as below
C:\equity trading\stocks>javac -version
javac 1.6.0-beta2

C:\equity trading\stocks>java -version
java version "1.6.0-beta2"
Java(TM) SE Runtime Environment (build 1.6.0-beta2-b86)
Java HotSpot(TM) Client VM (build 1.6.0-beta2-b86, mixed mode, sharing)

Now you can identify your JDK version based on class file format version whenever you see java.lang.UnsupportedClassVersionError :)

So next time when you see UnsupportedClassVersionError don't be afraid and follow the best approach based upon your need.

If you like this post you may find my other tutorial how java hashmap works, why String is immutable in java, why wait and notify needs to call from synchronized context  interesting.

15 comments :

Anonymous said...

Hi I am trying to run my program and getting below error:

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)
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)
Exception in thread "main"

Can you please suggest what is wrong ?

Sneha said...

I like very much your all explanation for answers about all java hectic questions. thank you so much for sharing your knowledge. It will definitely help us in interview.

Suraj said...

java.lang.UnsupportedClassVersionError Comes not because of different JDK at compile time at run-time but an higher JDK during compile time and lower JDK during runtime.

Anonymous said...

Does java.lang.UnsupportedClassVersionError: Bad version number in .class file is also comes due to same reason ? we are getting this error in Java and JSP code and appreciate your help .

Ricky said...

@Anonymous here is what Javadoc says "UnSupportedClassVersionError thrown when the Java Virtual Machine attempts to read a class file and determines that the major and minor version numbersin the file are not supported.

by the way "java.lang.UnsupportedClassVersionError: Bad version number in .class file" comes when you compile your source code in higher JDK and running them on lower JRE e.g. Compiling in JDK 1.6 and running on JRE 1.5

Anonymous said...

is it possible to use from 1.5 a child class loader with version java version 1.6.

I mean something what C/C++ can do when needs to use java code and runs a JVM in v 1.5 or 1.6, depending on needs.

Anonymous said...

Had on mac OSX

Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file

Problem was solved by:

Clearing the cache in Java preferences
open "/Applications/Utilities/Java Preferences.app"

Vividha said...

Real cause of java.lang.UnsupportedClassVersionError: Bad version number in .class file is having different version of Javac compiler and JRE version during build and deployment process. suppose you are creating your build with JDK 1.6 compiler but your web server or application server is running on JRE 1.5 which is lower source version than you will get this error whenever that particular class will be loaded.

Neha said...

I was getting this error as the system variables PATH, JAVA_HOME were not set correctly and I had not restarted my system after setting these.

Anonymous said...

Nice one..easy to understand.

Zach said...

I am getting java.lang.unsupportedclassversionerror unsupported major.minor version while building by Java application using ANT build file. My JDK version are 1.6.20, Please help me to fix this error.

Reena said...

Hello, What is cause of How to fix java.lang.unsupportedclassversionerror unsupported major.minor version 50.0, It Seems everything on my PATH and Classpath are fine and I am using Java 6 ?

Anonymous said...

Why java errors are so hard t solve. There are many Like this which takes long time to fix

Anonymous said...

Hello, I am getting following error message :
Exception in thread "main" java.lang.UnsupportedClassVersionError (Unsupported major.minor version 48.0), while running a Java application? We have Java version 1.5 installed on Server, Please advise?

Anonymous said...

I did this error, while working with Murex before, especially while installing Murex on one of our test box, which was running on Java 5 from long time. If you are installing MError message Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/lucene/document/Fieldable (Unsupported major.minor version 48.0) means that your java version is not suitable for Murex. Check if you have JDK 1.6 or not, because Murex currently support ava 1.6.* or higher.

Post a Comment