$show=/label

How to Fix java.lang.UnsupportedClassVersionError

SHARE:

Learn how to fix java.lang.UnsupportedClassVersionError and what are the causes for this runtime exception/error.

1. Introduction


In this article, We're going to learn how to fix the common runtime error java.lang.UnsupportedClassVersionError

From API: Thrown when the Java Virtual Machine attempts to read a class file and determines that the major and minor version numbers in the file are not supported.

How to Fix java.lang.UnsupportedClassVersionError?The simple cause is saying code is compiled at a higher version and running with lower java version. The solution is either run the code with the latest java version or compile the code with older JDK.

But let us see in detail what is the exact cause and how to fix it in various tools such as in command line, Eclipse and IntelliJ Idea tools.





2. Runtime Error


Now look at the error and try to understand the reason behind it. It is clearly stating that class is compiled in one version and you are trying to run in a different version.

Precisely saying that always lower version compiled class file can be run using any higher version or same as compiler java version. But why the error is coming in real and what is the exact cause. If a class is compiler at a higher version and tried to run with a lower version of java.

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/java/w3schools/blog/arraylist/ArrayListAddPrimitiveInt 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
 at java.lang.ClassLoader.defineClass1(Native Method)
 at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
 at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
 at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
 at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
 at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
 at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
 at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)


3. Look at the java version numbers


Every java release will come with a standard major version number. This will be handy when dealing with error messages.

Note: A compiled version of java file will be storing the major and minor version in class byte code at positions 6 and 7.

Major or Minor versions mapping to a Particular java version.

45 = Java 1.1
46 = Java 1.2
47 = Java 1.3
48 = Java 1.4
49 = Java 5
50 = Java 6
51 = Java 7
52 = Java 8
53 = Java 9
54 = Java 10
55 = Java 11
56 = Java 12
57 = Java 13
58 = Java 14

4. Fix Via Command Lines


Let us see how we can resolve this error using the command line in mac or windows.

Based on your situation, this problem can be solved in two wats.

A) Compile the source using an earlier version of java that shown in the error message.
B) Need to run the class file in new java version

But, the Decision is yours which to use. If you are using a third-party library that is compiled in the higher version then better to upgrade the java version. If you packing the jar for distributed usage then you need to use the older java version.

In my opinion, It is better to use the newer java version.

4.1 Check and update the java version


Run the command to see the installed java version and set to JAVA_HOME

echo %JAVA_HOME%
C:\softwares\Java\jdk8-x64

If you want to update the java version then install the latest java version and set to JAVA_HOME.

How to set environment variables for Java using the command line? Read More

setx -m JAVA_HOME "C:\Program Files\Java\jdk-11.0.2"
setx -m PATH "%PATH%;%JAVA_HOME%\bin";

4.2 Running on new JRE


Once java is upgraded and set JAVA_HOME then run the code against the new JRE.

Run the code with JRE 11.

C:\Program Files\Java\jdk-11.0.2\bin\java com.java.w3schools.blog.arraylist.ArrayListAddPrimitiveInt 
1
2
3
4
5
6
7
8
9

4.3 Compile Code With Older JDK


Compile the code with older jdk and run the code with the same version.

C:\softwares\Java\jdk8-x64\bin\javac com.java.w3schools.blog.arraylist.ArrayListAddPrimitiveInt.java

Let us make the code compatible with a higher version using --release parameter.

javac --release 8 com.java.w3schools.blog.arraylist.ArrayListAddPrimitiveInt.java



5. Eclipse IDE


Need to change the JRE to a higher version or Java compiler to an older version in eclipse project settings. Select the project press alt + enter (windows) or command + i (mac) to open properties.

5.1 JRE to Latest


Setting up the project JRE to newer.

JRE to Latest



5.2 JDK compiler to Older

Changing the java compiler to the lower version of our project.

JDK compiler to Older


6. IntelliJ Idea


As seen in eclipse, showing the changes for IntelliJ users.

6.1 JRE to Latest


Run -> Edit Configurations… and change our JRE to 11:

JRE to Latest


6.2 JDK compiler to Older



File -> Project Structure… -> Project Settings -> Project and change our Project SDK and Project language level:


Changing the Compiler Level

7. Maven Properties


Till Java 8:


When using Java 8 or older, we set the source and target for the compiler plugin in the pom.xml file.

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>

Java 9 Onwards:


New parameter –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: This is quite simpler.


<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>

8. Conclusion


In this article, we have seen the common JDK or JRE mismatch issue at runtime and java.lang.UnsupportedClassVersionError using command prompt, in eclipse IDE and IntelliJ Idea.


References:

Baeldung
Java API
JavaRevisited blog
StackOverFlow

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: How to Fix java.lang.UnsupportedClassVersionError
How to Fix java.lang.UnsupportedClassVersionError
Learn how to fix java.lang.UnsupportedClassVersionError and what are the causes for this runtime exception/error.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIXv3xePXfe5lRbhQxNfMvEqkjb75_YhIIdyLoDisyyrCa41DJllbLDdpg3FH40znSlvRilMR6QedPPzb3N23fmFsM-OyDB0xXiokApIEVZzOHuULU8IMTxDKIH1furFpIfPXnXXcLhMY/s640/JRE+to+Latest.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIXv3xePXfe5lRbhQxNfMvEqkjb75_YhIIdyLoDisyyrCa41DJllbLDdpg3FH40znSlvRilMR6QedPPzb3N23fmFsM-OyDB0xXiokApIEVZzOHuULU8IMTxDKIH1furFpIfPXnXXcLhMY/s72-c/JRE+to+Latest.png
JavaProgramTo.com
https://www.javaprogramto.com/2020/03/java-lang-unsupportedclassversion.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/03/java-lang-unsupportedclassversion.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content