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.
5.2 JDK compiler to Older
Changing the java compiler to the lower version of our project.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:
6.2 JDK compiler to Older
File -> Project Structure… -> Project Settings -> Project and change our Project SDK and Project language 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>
<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
No comments:
Post a Comment
Please do not add any spam links in the comments section.