Saturday, September 30, 2023

How to resolve java.lang.UnsupportedClassVersionError:Unsupported major.minor version 49.0 to 65 with example

The 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 are a class file format and version numbers associated with it and finally how to resolve UnsupportedClassVersionError in Java? 

 

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.


Now, let's deep dive in 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. 

The 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 or Oracle 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 it's 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 a class file with a higher version and when a lower JDK is used to run the program it found that a higher version of the class file not supported at JVM level and results in java.lang.UnsupportedClassVersionError.

For example if you use JDK 11 to compile your application and then try to run it on JDK 8 runtime or JRE then it will throw this error as it doesn't know anything about new features introduced in JDK 11 like HttpClient so it will not able to run the program. 

Same goes if you compile your program in Java 17 and then try to run on Java 11, you will get same error but with different class version. 

How to fix UnSupportedClassVersionError

Now we know the root cause of UnSupportedClassVersionError that we are using a lower JVM for running the program. But a 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 the latest version of JDK is 1.6 so the maximum version of the class file could be generated by JDK 6, so by using JDK 6 we can solve UnSupportedClassVersionError, but many times it's 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 a 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
JDK 1.7 = 51
JDK 8 = 52
JDK 9 = 53
JDK 10 = 54
JDK 11 = 55
JDK 12 = 56
JDK 13 = 57
JDK 14 = 58
JDK 15 = 59
JDK 16 = 60
JDK 17 = 61
JDK 18 = 62
JDK 19 = 63
JDK 20 = 64
JDK 21 = 65

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 on how java hashmap works, why String is immutable in java, why wait and notify needs to call from synchronized context interesting.

25 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.

Anonymous said...

Hello admin, I am getting "Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld : Unsupported major.minor version 51.0" error while trying to run my HelloWorld class from command line. Can you please help to fix this error?

Anonymous said...

Hello there, I am getting "java.lang.unsupportedclassversionerror unsupported major.minor version 50.0" while running a Java program in our production environment. We are still running on Java 1.5 but the build used to work before.

Anonymous said...

Another probable case of seeing this error is, when you have a serialization issue.

Unknown said...

Great post, than you very much!

to view said...

Rework on the settings and attach the compiler same as mentioned in Properties->Java Compiler, of eclipse setup. Rebuild the project. If necessary delete the class files manually and rebuild the project

javin paul said...

@Vijay Kulkarni, didn't get the context, are you describing how did you fix the java.lang.UnSupportedClassVersionError in Eclipse IDE?

Anonymous said...

Hello there, I am getting "java.lang.unsupportedclassversionerror unsupported major.minor version 50.0" while running a Java program in our production environment. We are still running on Java 1.5 but the build used to work before.


Unknown said...

Thank you very much!

Anonymous said...

How to customize this error and show a validation message to user so that the user can understand about Java version compatibility?

Anonymous said...

Thankfulness to my father who informed me on the
topic of this blog, this weblog is truly awesome.

Post a Comment