In this short tutorial, we're going to learn what causes the Java runtime error java.lang.UnsupportedClassVersionError: Unsupported major.minor version and how to fix it.
Let's start by looking at an example error:
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/baeldung/MajorMinorApp 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
This error is telling us that our class was compiled at a higher version of Java than the version with which we tried to run it. More specifically, in this case we compiled our class with Java 11 and tried to run it with Java 8.
For reference, let's take a quick look at the Java version numbers. This will come in handy in case we need to download the appropriate Java version.
The major and minor version numbers are stored in the class bytecode at bytes six and seven.
<iframe data-google-container-id="2" data-is-safeframe="true" data-load-complete="true" frameborder="0" height="90" id="google_ads_iframe_/15184186,7114245/baeldung_leaderboard_mid_1_0" marginheight="0" marginwidth="0" scrolling="no" src="https://49c23aecf4e50cad88bac951f043eba9.safeframe.googlesyndication.com/safeframe/1-0-38/html/container.html" title="3rd party ad content" width="728"></iframe>Let's see how the major version numbers map to Java versions:
Now let's discuss how we can resolve this error when running Java from the command line.
Depending on our situation, we have two ways we can resolve this error: compile our code for an earlier version of Java, or run our code on a newer Java version.
The final decision depends on our situation. If we need to use a third-party library that's already been compiled at a higher level, our best option is probably to run our application using a newer Java version. If we're packaging an application for distribution, it might be best to compile down to an older version.
Let's start by checking how our JAVA_HOME variable is set. This will tell us which JDK is being used when we run javac from our command line:
<iframe data-google-container-id="3" data-load-complete="true" frameborder="0" height="1" id="google_ads_iframe_/15184186,7114245/baeldung_leaderboard_mid_2_0" marginheight="0" marginwidth="0" name="google_ads_iframe_/15184186,7114245/baeldung_leaderboard_mid_2_0" scrolling="no" title="3rd party ad content" width="1"></iframe>echo %JAVA_HOME% C:\Apps\Java\jdk8-x64
If we're ready to move entirely to a newer JDK, we can download the newer version and make sure our PATH and JAVA_HOME environment variables are set appropriately.
Returning to our example, let's look at how we can resolve the error by running it at a higher version of Java. Assuming we have Java 11 JRE in C:\Apps\jdk-11.0.2, we can run our code with the java command packaged with it:
C:\Apps\jdk-11.0.2\bin\java com.baeldung.MajorMinorApp Hello World!
If we're writing an application that we want to be runnable down to a certain version of Java, we need to compile the code for that version.
We can do that in one of three ways: using an older JDK to compile our code; using the -bootclasspath, -source, and -target options of the javac command (JDK 8 and older); or using the –release option (JDK 9 and newer).
Let's start by using an older JDK, similar to how we used a newer JRE for running our code:
<iframe data-google-container-id="4" data-is-safeframe="true" data-load-complete="true" frameborder="0" height="280" id="google_ads_iframe_/15184186,7114245/baeldung_leaderboard_mid_3_0" marginheight="0" marginwidth="0" scrolling="no" src="https://49c23aecf4e50cad88bac951f043eba9.safeframe.googlesyndication.com/safeframe/1-0-38/html/container.html" title="3rd party ad content" width="970"></iframe>C:\Apps\Java\jdk1.8.0_31\bin\javac com/baeldung/MajorMinorApp.java
It's possible to just use -source and -target, but it might still create class files that aren't compatible with an older Java.
To ensure compatibility, we can point -bootclasspath at the rt.jar of the targeted JRE:
javac -bootclasspath "C:\Apps\Java\jdk1.8.0_31\jre\lib\rt.jar" \ -source 1.8 -target 1.8 com/baeldung/MajorMinorApp.java
The above applies mainly to JDK 8 and lower. In JDK 9, the –release parameter was added to replace -source and -target. The –release option supports targets 6, 7, 8, 9, 10, and 11.
Let's use –release to target Java 8:
javac --release 8 com/baeldung/MajorMinorApp.java
Now we can run our code on a Java 8 or higher JRE.
Now that we understand the error and the general approach to correcting it, let's take what we've learned and see how we can apply it when working in the Eclipse IDE.
Assuming we already have Eclipse configured with different versions of Java, let's change our project's JRE.
Let's go to our Project properties, then Java Build Path, and then the Libraries tab. Once there, we'll select the JRE and click Edit:
<iframe data-google-container-id="5" data-is-safeframe="true" data-load-complete="true" frameborder="0" height="250" id="google_ads_iframe_/15184186,7114245/baeldung_incontent_1_0" marginheight="0" marginwidth="0" scrolling="no" src="https://49c23aecf4e50cad88bac951f043eba9.safeframe.googlesyndication.com/safeframe/1-0-38/html/container.html" title="3rd party ad content" width="300"></iframe>Now let's choose Alternate JRE and point to our Java 11 installation:
At this point, our application will run against Java 11.
Now let's look at how we can change our target to a lower level of Java.
First, let's go back to our Project properties, then Java Compiler, and then check Enable project specific settings:
Here we can set our project to compile for earlier versions of Java and customize other compliance settings:
We can also control the version of Java we're using for compiling and running in IntelliJ IDEA.
Before we do that, we'll see how to add additional JDKs. Let's go to File -> Project Structure -> Platform Settings -> SDKs:
Let's click the plus icon in the middle column, select the JDK from the drop-down, and select our JDK location:
<iframe data-google-container-id="6" data-load-complete="true" frameborder="0" height="1" id="google_ads_iframe_/15184186,7114245/baeldung_incontent_2_0" marginheight="0" marginwidth="0" name="google_ads_iframe_/15184186,7114245/baeldung_incontent_2_0" scrolling="no" title="3rd party ad content" width="1"></iframe>First, we'll look at how to use IDEA to run our project on the newer JRE.
Let's go to Run -> Edit Configurations… and change our JRE to 11:
Now when we run our project, it will run with the Java 11 JRE.
If we're distributing our application to run on a lower JRE, we need to adjust our compiler level to target the older version of Java.
Let's go to File -> Project Structure… -> Project Settings -> Project and change our Project SDK and Project language level:
We can now build our project, and the class files generated will run on Java 8 and higher.
When we build and package a file in Maven, we can control the version of Java we target.
When using Java 8 or older, we set the source and target for the compiler plugin.
<iframe data-google-container-id="7" data-load-complete="true" frameborder="0" height="1" id="google_ads_iframe_/15184186,7114245/baeldung_incontent_3_0" marginheight="0" marginwidth="0" name="google_ads_iframe_/15184186,7114245/baeldung_incontent_3_0" scrolling="no" title="3rd party ad content" width="1"></iframe>Let's set the source and target using compiler plugin properties:
<properties> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> </properties>
Alternatively, we can set the source and target in the compiler plugin:
<plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins>
With the –release option added in Java 9, we can configure that with Maven as well.
Let's use a compiler plugin property to set the release:
<properties> <maven.compiler.release>8</maven.compiler.release> </properties>
Or we can configure the compiler plugin directly:
<plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <release>8</release> </configuration> </plugin> </plugins>
In this article, we learned what causes the java.lang.UnsupportedClassVersionError: Unsupported major.minor version error message, and how to fix it.