The java.lang.UnsupportedClassVersionError is a sub-class of LinkageError and ClassFormatError. The JVM (Java Virtual Machine) throw this error when try to read a file but Major and Minor Java version are not supported.
This Java Major and Minor class error occurred while running through IDE eclipse/STS or any because you JAVA_HOME or path or IDE Build Path is configured with different version of Java (JDK/JRE) however your code is compiled with different version of IDE. or Maven pom.xml is configured with different java version while running the program over different JDK/JRE which is not match with Major and Minor version supported.
In further post you will get to know about the resolution steps.
Example of Error Messages:
java.lang.LinkageError: XYZ has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
java.lang.UnsupportedClassVersionError: XYZ has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
class file has wrong version 61.0, should be 55.0
Java Major Version
These are the Java Major Versions
How to check Java Major Version?
Yu can run the below command in your command prompt.
javap -v <path to class file> | grep "major"
$ javap -v build/classes/com/FacingIssuesOnIT/Book.class | grep “major”
How to fix Java Major and Minor Version Issue?
Follow these steps to resolve Java Major & Minor version issue:
Step 1: Go to command prompt and check for Java version
Step 2: In case your system is having more than one version of Java and you want to run your application or code with specific version, then go to Application-> Right click->Select Properties->Go to Build Path->Go to Library and remove the default Library from Class Path and Module Path.
Step 3: Click on Add Library Button and Select JRE System Library, click on Next button. If your JRE on default then select or go to alternate JRE and select Java path as below by Browse button. Click on Apply and Close button and then Finish button
Step 4: Now go to the left panel and select correct compiler version as below and click on apply button.
Step 5: If your application is Maven based then you have to make additional changes in your pom.xml also
<properties> <java.version>20</java.version> </properties>
<plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>20</source> <target>20</target> </configuration> </plugin> </plugins>
Now save all your changes and run your application or program. It will work fine.
In this post you have learned about the Java Major and Minor version and also to check way of checking through command prompt. If you are facing such exceptions also provided way to resolve it.