In javajava.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.

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, 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 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.
[ad type=”banner”]

When UnSupportedClassVersionError in Java comes:

  • “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”.
  • Important 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

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

Example 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 to 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

[ad type=”banner”]

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.

 

  • Unsupported major.minor version 51.0 error comes when you run a class file created using Java 1.7 (major version 51.0) into a lower JRE version e.g. JRE 6, 5 or 4.
  • There are two ways to solve this problem, first make sure you run your Java program in same or higher version of JRE, on which it has compiled, and second use cross-compilation option to create a class file compatible to a lower JRE.
  • Use javac -target option to create a class file for all JRE up-to-the JDK version you are using for compilation.
  • For example, if we are compiling our Java source file into JDK 1.7 then we can create class files to run on JDK 1.1, 1.2, 1.3, 1.4 , 1.5 and 1.6 version, but we cannot create class files compatible with Java 1.8.
  • When we compile a Java source file, it creates a class file and add the class file version into it. In order to run that class file, our JRE must understand that version.
  • When JRE or JVM which is running the class doesn’t able to understand the class file version they throw java.lang.UnsupportedClassVersionError: XXX : Unsupported major.minor version 51.0 error, where XXX is the name of your class which has an incompatible version.

Solution 1 – Upgrade JRE

  • Simplest solution to fix Unsupported major.minor version 51.0 error changes your Java virtual machine.
  • Upgrade JRE to get a version same or higher than the version we have used for compiling the project.
  • Once we got that, just run your program again. It should work fine this time.
  • Since java.lang.UnsupportedClassVersionError: Unsupported major.minor version 51.0 clearly says that this file needs, at least, Java 7 or higher to run.
  • We need to install 32-bit or 64-bit JDK 1.7 or JRE 1.7 version depending upon whether you are running on 32-bit or 64-bit OS e.g. If we are running on Windows 7 or Windows 8 then install a 64-bit version of JDK.
  • We would also be fine if we are running on Java 8, but don’t run the program on Java 6 or lower version.

Solution 2 – Use javac -target option

  • The root cause of any java.lang.UnsupportedClassVersionError: Unsupported major.the minor version is incompatible JRE version at run-time, but changing JRE is not the only solution we can even compile your class file for lower JRE version. In order to adapt this solution, we need to re-compile our project.
  • If recompilation is an option then make sure we add -target option to javac with corresponding Java version we are looking as target runtime.

For example, if we are generating class file to run on Java 6, pass the following argument to javac command :

[pastacode lang=”java” manual=”javac%20-target%201.6%20*.java” message=”Java Code” highlight=”” provider=”manual”/] [ad type=”banner”]

*.java is to compile all Java files and target 1.6 means the class file will be compatible with Java SE 6 JVM. If we want to compile a class file for Java 5, just use -target 1.5 option.

How to fix Unsupported major.minor version 51.0 in Eclipse

  • Unsupported major.minor version 51.0 error while running your project in Eclipse, then we need to change compiler option for the project in Eclipse.
  • This actually happens when we have a project specific compiler version setting but running on Eclipse’s default JRE. If we later changed Eclipse default JRE from JDK 1.7 to JDK 1.6, we will see following error :

java.lang.UnsupportedClassVersionError: dto/ReverseArrayInPlace : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) at java.net.URLClassLoader.access$000(URLClassLoader.java:58) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) Exception in thread “main”

Categorized in: