Monday, August 2, 2021

Fixing Unsupported major.minor version 52.0 Error in Java

Unsupported major.minor version 52.0 comes when you are trying to run a class compiled using Java 1.8 compiler into a lower JRE version e.g. JRE 1.7 or JRE 1.6. Simplest way to fix this error is install the latest Java release i.e. Java 8 and run your program. If upgrading to Java 8 is not an option then make sure you use Java compiler's cross compilation feature and compile source code for lower Java version by using -target option of javac command. If you have multiple JRE installed make sure that the latest one comes first in PATH environment variable.

You can get "java.lang.unsupportedclassversionerror unsupported major.minor version 52.0" while running a Java program explicitly using java command or running a Java Applet in browser or running a Java program from command line but compiled in Eclipse or any other build tool like Maven or ANT.

The Main reason of this error is that during compilation, you have used a higher version of JDK but during deployment, you have deployed into a lower version of JDK or JRE. Let's understand the cause and solution of Unsupported major.minor version 52.0 Error in little more detail.

TL:DR; upgrade to Java 8 or compile for lower JRE version using java -target 1.6 option.


Cause of Unsupported major.minor version 52.0 Error in Java

Many people think why do you get a version mismatch error if Java is backward compatible. Well, it's true that Java is backward compatible, which means you can run a Java class file or Java binary (JAR file) compiled in lower version (java 6) into higher version e.g. Java 8, but it doesn't mean that you can run a class compiled using Java 7 into Java 5, Why? because higher version usually have features which are not supported by the lower version.



Think about, can you run lambda expression or Stream API code in Java 5, no right. Anyway, even if you are not using features which is not supported in a lower version, every class file has a major or minor version, which is populated by Java compiler, which is closely related to Java version. Here are the major version of every JRE released so far :

Java SE 8 = 52,
Java SE 7 = 51,
Java SE 6.0 = 50,
Java SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45

You can see that Java 8 has major version 52, which means if you run javac command from Java 8 installation, it will by default generate a class with major version 52. If you run this class file in JRE 7, you will get "Unsupported major.minor version 52.0". Same is the case with an applet compiled in JDK 1.8, running in a browser with JRE 1.7.

BTW, Java allows you to generate class files supported by lower version. So, for example, you can generate a Java class file for JDK 1.4 by running javac command with -target option as shown below :

javac -target 1.4 HelloWorld.java

The Helloworld.class file generated by this command can be run in any version of Java 1.4 onward e.g. Java 1.4, Java 5 or Java 6.



Solution of Unsupported major.minor version 52.0 Error in Java

As I said earlier, there are two ways to fix "Unsupported major.minor version 52.0" error, first upgrade to higher Java version or compiler Java source files for lower Java version of target environment. If you are getting "java.lang.unsupportedclassversionerror HelloWorld unsupported major.minor version 52.0", which means Java version in your PATH is lesser than Java 8.

If you already installed the JDK 1.8 then its due to incorrect PATH setting. If you have multiple JRE version installed then lower version is coming ahead in PATH than higher version. To fix this, examine your PATH environment variable and make sure the latest version of Java comes first. You don't need to touch your CLASSPATH, because its not a classpath issue. Remember, difference between PATH and CLASSPATH.

If you cannot upgrade to higher Java version e.g. your production is running on JDK 1.6 and your latest release is failing due to "Unsupported major.minor version 52.0" error then you need to fix your build environment.

Make sure to install the same JDK version for building your Java application as you are going to run in production. If that's not possible, use Java compiler's cross compilation option to generate class file for your production Java/JRE version using javac -target option. For example, if your build environment is using Java 7 and your production environment is using Java 1.6 then compile using java -target 1.6 option to generate class files for Java 6.


In Applet

If you are trying to run an Applet from a website (internal or external) then you don't have any control on build or compile step. Your only option is to upgrade the JRE plugin of your browser to Java 8. Once you done that you should be able to run the Applet compiled in any Java version without any issue, remember Java is backward compatible. 

BTW, from Java 8 onward security has been tighten and you cannot run an unsigned or self signed Applet in Java, especially if you have HIGH security setting in Java Control Panel. Java 8 has also removed the MEDIUM security settings, which earlier shows warning when you run such Applets.


In Eclipse

If you are compiling your Java program in Eclipse and running it from command line, then you have the option to compile Java files for target environment. This is also necessary if you are exporting JARs from Eclipse. You can change compiler setting in Eclipse by selecting your project, properties, java compiler and unchecked JDK compliance checkbox to change the Compiler compliance level.

By default Eclipse compiles based upon JRE version of the project, which could be different in Java in your PATH, which comes into play when you run your Java program from command line. Here is the screenshot of changing compiler compliance level in Eclipse :
  • Select your project
  • Right click, select properties
  • Choose Java Compiler
  • Uncheck JDK compilance check box

java.lang.unsupportedclassversionerror unsupported major.minor version 52.0

That's all about how to fix an Unsupported major.minor version 52.0 Error in Java. In short, you are getting this error because it is compiled with Java 8, source version 52 corresponds to Java 1.8 release but you are trying to run this program in lower JRE e.g. Java 7 or Java 6.

This means you have two solutions, first upgrade to Java 8 or compiler your program for lower version even if you are using Java 8 compiler by using -target option of javac command. If you are using Maven, ANT, or any other build tool to generate your class files, make sure they have got the right settings too.


17 comments :

azkar said...

Hi Javin. First of all many thanks for creating such a great site for learning java and passing interviews. Menu on the left was very helpful as it was topic based. Please restore that if possible.

Anonymous said...

thanks a ton, I was getting very same error in Eclipse and your solution saved my day.

Anonymous said...

@Anonymous, In Eclipse if you get UnsupportedClassVersionError like below
"java.lang.UnsupportedClassVersionError: Helloworld : Unsupported major.minor version 51.0"
then it could also be due to your compiler setting is set to JDK 1.7 compliance level and you are using project JRE or default JRE which is now pointing to Java 1.6 version.

If you click on run configuration, Eclipse will even show warning that your JRE is not comptablie with project .class file compatibility 1.7

Unknown said...

The Eclipse / Java Compiler part helped.. Thanks!!

Anonymous said...

Thanks lot. You saved my day.

Unknown said...

i built and ran the application using jdk1.8 but still i am seeing the same issue.
for java version 1.8 : Unsupported major.minor version 52.0

Danielson Junior said...

Thank you! Help a lot, bro!!

Anonymous said...

thank you.

Unknown said...

still problem occurs what should i do

Gayathri Ramamurthy said...

This indeed resolved the issue 1) installed JDK 1.8 version in windows 7 2) set PATH environmental variable in system to point to path where java is installed

Anonymous said...

Hi,
I compiled my program in 1.7 only and using JRE7 only


still it is not this exception is coming.

Can anyone help?

Unknown said...

Thanks a lot Javin, i was not able to understand the root cause of this problem in my eclipse. got the solution. it works fine now.

Unknown said...

Really thank you. You save my day.

Anonymous said...

I already use java 8 but jasper report is always major major 52, what do I have to do

Unknown said...

i have JDK 15 installed, how can i set the eciplse and intellij ide to compile with jre 59

Anonymous said...

The following steps helped me :)
1. list all the jdk installed using the command
# rpm -aq | grep -i jdk
2. Uninstall the unwanted jdks (if any)
# sudo yum remove
3. Set the JAVA_HOME path to desired jdk path. Follow https://confluence.atlassian.com/crowd/setting-java_home-61604243.html

javin paul said...

Hello Anonymous, Are you trying this on Mac OS or Linux? You don't really need to uninstall all the JDKs, you could have just removed them from PATH.

Post a Comment