The java.lang.UnsupportedClassVersionError happens when your code is compiled with one Java version but runs with another. This error means that the Java versions do not match, and your program won’t run.
In this blog, we will discuss how to fix the java.lang.UnsupportedClassVersionError in detail.
Table of Contents:
What is the UnsupportedClassVersionError?
UnsupportedClassVersionError occurs when a Java program is compiled with one version but executed with another.
For example, if you compile a Java program with JDK 17 and try to run it using JRE 11, an error will occur because JRE 11 does not understand the bytecode generated by JDK 17. To fix this, ensure that your JRE version matches or is higher than the JDK version used for compilation.
What Causes java.lang.UnsupportedClassVersionError?
This error mainly occurs when the Java versions are not the same, like when the program is compiled with different Java versions and executed in different versions.
Java is a backward-compatible programming language, which means a Java class file or binary file (JAR file) compiled on a lower version of Java can be executed on a higher version. However, the opposite is not true, i.e., a class compiled on a higher version of Java cannot be executed on a lower version. This is because the higher version contains features that are not present in the lower versions. Due to this, the java.lang.UnsupportedClassVersionError occurs.
Master Java Today - Accelerate Your Future
Enroll Now and Transform Your Future
Java Version Numbers
Let us discuss some of the Java versions that are required to see when the java.lang.UnsupportedClassVersionError occurs.
The following table shows the Java versions and their class file versions:
Java Version |
Class File Version |
Java 1.1 |
45 |
Java 1.2 |
46 |
Java 1.3 |
47 |
Java 1.4 |
48 |
Java 5 |
49 |
Java 6 |
50 |
Java 7 |
51 |
Java 8 |
52 |
Java 9 |
53 |
Java 10 |
54 |
Java 11 |
55 |
Java 12 |
56 |
Java 13 |
57 |
Java 14 |
58 |
Java 15 |
59 |
Java 16 |
60 |
Java 17 |
61 |
Java 18 |
62 |
Java 19 |
63 |
Java 20 |
64 |
Java 21 |
65 |
Java 22 |
66 |
Java 23 |
67 |
Java 24 |
68 |
With the help of the above table, you will get to know about Java version in which your code was compiled. After which, you can install the dedicated Java version on your system.
How to Fix java.lang.UnsupportedClassVersionError
To fix this error, you should make sure that the Java version with which you are compiling the project is the same as the version with which you are executing your project.
1. Check the Java Version Used to Compile the Code
You can run the following commands to check the Java version
java -version
or
javac -version
2. Compiling the code with an older version of JDK via the Command Line
If the above JRE is not possible, you can downgrade the JDK to the same version as the production. Use the following command to compile the Java program to a specific version
javac --release X YourFile.java
where,
X is the Java version
YourFile is the name of the Java file
3. Ensure that you’re using the correct JAVA_HOME and PATH
The JAVA_HOME environment variable tells your system where Java is installed, while the PATH variable ensures that the Java commands like java and javac can be executed from any location in the terminal or command prompt.
If these are not set correctly for your system, it can use another Java version or will fail to understand the Java commands altogether. Hence, set JAVA_HOME to the correct JDK location.
4. Update the default Java version
The JRE version must be upgraded to the latest release or should be the same as the build environment. If multiple Java versions are installed on your system, then the default version used for execution and compilation can be changed. To ensure the correct version is used, you need to update the default Java version on your system.
5. Recompiling by Targeting an Older Java Version
The Java compiling option can also be used to generate the .class file by using the javac-target command. For example, if the Java environment is using Java 1.8 for compiling and production is using Java 1.7, the following command can be used to generate a class file of Java 1.7.
javac -source 1.7 -target 1.7 <path-to-java-class>
Unlock Your Future in Java
Start Your Java Journey for Free Today
6. Configure your IDE for a specific Java version
If your IDE is not using the correct Java version, it can cause issues and runtime errors. You should configure your IDE to one Java version.
Steps to Configure Java Version in Different IDEs
IntelliJ IDEA
- Check and Set up the JDK:
- Open File > Project Structure > Project.
- Under Project SDK, select the Java version or click Add JDK to add a new one.
- Check Compiler Settings:
- Go to File > Settings > Build, Execution, Deployment > Compiler > Java Compiler.
- Set the Target bytecode version to be the same as your Java version.
- Check the Run Configuration:
- Open Run > Edit Configurations.
- Ensure the correct JDK is selected.
Eclipse
- Check Installed JREs:
- Go to Window > Preferences > Java > Installed JREs.
- Add the JDK if it’s not listed and select it as the default.
- Set JDK for a Specific Project:
- Right-click the project, select Properties > Java Build Path.
- Under the Libraries tab, remove the old JDK and add the correct one.
- Configure the Compiler Level:
- Go to Window > Preferences > Java > Compiler.
- Set the Compiler level to your JDK version.
VS Code
- Ensure Java Extension is Installed:
- Install the Java Extension Pack from the VS Code marketplace.
- Set Java Home in VS Code Settings:
- Open Settings (Ctrl +, or Cmd +, on macOS).
- Search for Java: Home.
- Set it to the correct JDK path (e.g., C:Program FilesJavajdk-17).
NetBeans
- Check and Set JDK for NetBeans:
- Open Tools > Java Platforms.
- Add and select the JDK version.
- Set Java Version for a Project:
- Right-click the project and go to Properties > Libraries.
- Select the Java platform.
After the above steps, verify the IDE configuration using the following command.
java -version
javac -version
7. Using Maven
When using Maven, to fix the java.lang.UnsupportedClassVersionError: Unsupported major. Minor version error, you should ensure that the Java version used to compile the code matches the runtime version of Java.
Also, the dependencies in the Maven are properly added as per the correct Java version.
Add the following dependencies to the pom.xml file of your Maven project.
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version> <!-- Use latest -->
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
Note: Replace the 11 with your Java version.
Now, run the Clean and Rebuild Command at the top right in your IDE for the Maven project.
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
The java.lang.UnsupportedClassVersionError occurs when Java compiles a program with a newer version, but runs it on an older one. To fix this, you should use the same or a higher JRE, set the correct JAVA_HOME, or compile the program with an older version. Also, update your IDE settings to match the right Java version.
To know more about this topic, you can refer to our Java Course.
How to Fix java.lang.UnsupportedClassVersionError – FAQs
Q1. How to fix the java.lang.UnsupportedClassVersionError version error?
This error can be fixed by running the application on the same (or higher) JRE version as the JDK.
Q2. What is the major.minor version in Java?
A major.minor version error occurs when the Java Runtime Environment (JRE) that is used to run a program is older than the Java Development Kit (JDK) version that is used to compile its code.
Q3. How do I check the Java version?
You can check the Java version with the following command in your command prompt
java -version
Q4. What is the latest version of Java?
As of April 2, 2025, the latest version of Java is Java 24 (JDK 24), which was released on March 18, 2025. Check the official Oracle website for the latest updates, as Java versions are released regularly.
Q5. What is the difference between JRE and JDK?
The Java Development Kit (JDK) is used to develop Java applications, while the Java Runtime Environment (JRE) provides class libraries and other things that are needed to run Java applications on the Java Virtual Machine (JVM).