java.lang.UnsupportedClassVersionerror and its solution

Almost every java developer would have encountered the below error sometime or the other while executing a java program.

Exception in thread “main” java.lang.UnsupportedClassVersionError: com/codippa/HelloWorld
has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

java.lang.UnsupportedClassVersionError means that the version of JDK on which the java file was compiled was a higher version and the JRE version on which it is being executed is of a lower version. Example, a file compiled on JDK 8 is being executed on JRE 6.

And that is also what the error clearly states “has been compiled by a more recent version of the Java Runtime”.

To learn how to check the version of JDK compiler used to compile a class, refer this post.

Following points should be noted regarding java.lang.UnsupportedClassVersionError,
It extends java.lang.Error and hence it cannot be handled by the application code.
It will never occur at compile time and will always occur at execution time.
It simply means that compiling Java environment was at a higher version as compared to executing Java environment.

Solution
There are following ways to solve this error.
1. Downgrade the compiler version
If you have the source(java code) of the class that raises the error, then compile it again with the same version on which you will be running it.

2. Upgrade the runtime version
If you do not have the source of the class file or the jars in classpath, then you need to upgrade the runtime environment version to match or surpass with the compiler version.
Every java class is assigned a Major version after it is compiled which directly tells about the version of compiling JDK used. Check this post.
Once you know the version on which the class was compiled, upgrade your JRE to that version and the class will execute fine.

3. Compile for a specific version
While compiling the code, specify the JRE version for which you want the code to be compiled using -target option of javac command. Example,

javac -target 1.7 com.codippa.HelloWorld.java

This will make the class compatible with JRE 7 and above.
-target option was valid till java 8. From java 9, this has been modified to --release. Thus, above command on java 9 and higher needs to be changed to

javac –release 1.7 com.codippa.HelloWorld.java

--release option will only support java 7 till 14, not below that.

If you are compiling the code in eclipse and want to compile it for a specific version, then Go to Window(menu option) -> Preferences, choose Java -> Compiler from the left menu and select the JDK compliance option as per the required version as shown below.
Setting target java compiler version in eclipse
Once you click on apply, the entire project will be compiled again for the version selected. If the drop down appears to be disabled, then click on Configure Project Specific Settings and Select Enable Project Specific Settings so that target compiler version for only a particular project is changed.
Hit the clap below if you liked this post.

sdasdasd

Leave a Reply