Unsupported major.minor version Error

The "Unsupported major.minor version" error occurs due to a mismatch in Java versions. This issue arises when a project is compiled on a higher Java version (e.g., JDK 1.8) and subsequently executed on a lower version (e.g., JDK 1.7). To address this error, there are two potential solutions: either recompile the code for an earlier Java version or run the code on a newer Java version. In situations where multiple Java SDK versions are installed on a system, it is crucial to ensure that the application being executed points to the appropriate or highest available version. For optimal compatibility, it is advisable to have both JRE/JDK components installed with the same version.


what is java.lang.UnsupportedClassVersionError: Unsupported major.minor version

Java minor_version, major_version

The minor_version and major_version items in a Java class file represent the minor and major version numbers of the file, respectively. These version numbers collectively define the .class file format version. In this format, the combination of a major version (M) and a minor version (m) indicates the version of the .class file. Consequently, class file format versions can be sorted lexicographically, enabling comparison based on version numbers, such as 1.5 < 2.0 < 2.1.

Major version number of the class file format being used.

  1. Java SE 14 = 58 (0x3A hex)
  2. Java SE 13 = 57 (0x39 hex)
  3. Java SE 12 = 56 (0x38 hex)
  4. Java SE 11 = 55 (0x37 hex)
  5. Java SE 10 = 54 (0x36 hex)
  6. Java SE 9 = 53 (0x35 hex)
  7. Java SE 8 = 52 (0x34 hex)
  8. Java SE 7 = 51 (0x33 hex)
  9. Java SE 6.0 = 50 (0x32 hex)
  10. Java SE 5.0 = 49 (0x31 hex)
  11. JDK 1.4 = 48 (0x30 hex)
  12. JDK 1.3 = 47 (0x2F hex)
  13. JDK 1.2 = 46 (0x2E hex)
  14. JDK 1.1 = 45 (0x2D hex)

Java Compatibility

Java indeed places a strong emphasis on maintaining backward compatibility to ensure smooth evolution within its ecosystem. However, there are instances where changes that are not backward compatible become necessary to drive progress. Despite being backward compatible, it's essential to understand that certain version mismatches may still occur. While Java allows running class files or JAR files compiled in lower versions (e.g., Java 6) on higher versions (e.g., Java 8), the reverse is not always feasible. Running a class compiled with Java 7 on Java 5 may lead to errors since higher versions often introduce features that are unsupported in lower versions. Thus, it's crucial to consider version compatibility when executing Java applications across different versions.

For e.g. "Unsupported major.minor version 52.0" error occurs when attempting to execute a class compiled using the Java 1.8 compiler on a lower version of the Java Runtime Environment (JRE), such as JRE 1.7 or JRE 1.6. To address this issue, two potential solutions exist. Firstly, you can run the Java code using a more recent version of the Java JRE that matches the version used for compilation. Alternatively, you may specify the "target" parameter when invoking the Java compiler to instruct it to produce bytecode compatible with earlier Java versions, thus resolving the compatibility mismatch and enabling successful execution on lower JRE versions.

Eclipse

If you're using Eclipse you should do 2 things:

In Eclipse, click on Window > Preferences , and in the window that appears, on the left side, under Java , click on Installed JREs , click on Add... and navigate to the folder that contains the JDK.


Eclipse  java.lang.UnsupportedClassVersionError: Unsupported major.minor version

Select the check box to activate the required version of JRE.

Right-click on your project and click on Properties , in the window that appears, on the left side, click on Java Compiler and uncheck Use compliance from execution environment on the Java Build Path, this allows you to choose in the list Compiler compliance level the same version that you set in the previous step.


NetBeans  java.lang.UnsupportedClassVersionError: Unsupported major.minor version

If the version of compiler compliance level and Installed JRE is different, eclipse gives the messages as follows. "When selecting 1.8 compliance, make sure to have a compatible JRE installed and activated (currently 1.7)"

Android Studio

If you have installed Android N , change Android rendering version with older one and the problem will disappear.


android java.lang.UnsupportedClassVersionError: Unsupported major.minor version

NetBeans

If you're using the NetBeans IDE , right click on the project and choose Properties and go to sources, and you can change the Source/Binary Format to a lower JDK version.

If you are using Maven , you can set the JDK version of each module by placing a file called nb-configuration.xml beside your pom.xml with the following content:

<?xml version="1.0" encoding="UTF-8"?> <project-shared-configuration> <properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1"> <netbeans.hint.jdkPlatform>JDK_1.7</netbeans.hint.jdkPlatform> </properties> </project-shared-configuration>

The "JDK_1.7" Java platform must be configured in Tools -> Java Platforms -> Add Platform .

Conclusion

The "Unsupported major.minor version" error occurs when trying to execute a Java class compiled with a higher version of the Java compiler on a lower version of the Java Runtime Environment (JRE). This error can be resolved by either using a newer version of the JRE matching the compiler's version or specifying the "target" parameter to the Java compiler to generate bytecode compatible with the desired JRE version.