How do I fix it?
This error means that the JRE that is being used to execute your class code does not recognise the version of Java used. Usually because the version of Java that generated your class file (i.e. compiled it) is newer.
To fix it, you can either
a) Compile your Java sources with the same, or older, version of the Java compiler as will be used to run it. i.e. install the appropriate JDK.
b) Compile your Java sources with the newer version of the Java compiler but in compatibility mode. i.e. use the -target
parameter.
c) Run your compiled classes in a JRE that is the same, or newer, version as the JDK used to compile the classes.
You can check the versions you are currently using with
javac -version
for the compiler, and java -version
for the runtime.
Should I install the JDK, and setup my PATH variable to the JDK
instead of JRE?
For compilation, certainly, install and configure the specific JDK that you want.
For runtime, you can use the one that comes with the JDK or a standalone JRE, but regardless, make sure that you have installed the right versions and that you have configured your PATH such that there are no surprises.
What is the difference between the PATH variable in JRE or JDK?
The PATH environment variable tells the command shell where to look for the command you type. When you type java
, the command shell interpreter will look through all the locations specified in the PATH
variable, from left to right, to find the appropriate java
runtime executable to run. If you have multiple versions of Java installed — i.e. you have the java
executable in multiple locations specified in the PATH variable, then the first one encountered when going from left to right will be the one that is executed.
The compiler command is javac
and only comes with the JDK. The runtime command is java
and comes with the JDK and is in the JRE.
It is likely that you have one version (51.0 = Java 7) of javac
installed, and you also have the same version of java
installed, but that another previous version of java
is appearing earlier in the PATH and so is being invoked instead of the one you expect.
The UnsupportedClassVersionError is a sub-class of the LinkageError and thrown by the Java Virtual Machine (JVM). When a class file is read and when major and minor version numbers are not supported, this error is thrown, and especially during the linking phase, this error is thrown
A sample snapshot of UnsupportedClassVersionError
Situations when the error is thrown :
When we tried to compile a program using a higher version of Java and execute it using a JVM of a lower version, this error is thrown. But the reverse case is not true.
Hence, it is important to note the java versions
Get the installed java version :
java –version (In command prompt(windows), we can give)
Check java version
Fixing the program via command line :
In order to overcome the UnsupportedClassVersionError, we can either compile our code for an earlier version of Java or run our code on a newer Java version. It is about our choice of decision only. If a third-party library is used then, it is better to run on a newer java version and if it is for distribution, it is best to compile to an older version.
Using JAVA_HOME Environment Variable :
While doing Java programs, always it is good to set JAVA_HOME and setting that value in path
We can check that in command prompt in the below way also
C:Windowssystem32>echo %JAVA_HOME% C:Program FilesJavajdk-9.0.4
Requirements to run a java program on a new JRE:
Move to the directory and locate the bin directory. For example, if the JRE is in location like
C:Program FilesJavajdk-11.0.2, then navigate till C:Program FilesJavajdk-11.0.2bin
Execute the program as
java <filename> // Even we can give filename with full package
Requirements to run a java program on an older JRE :
Example :
C:Program FilesJavajdk1.8.0_31binjavac <filename along with package>
In order to ensure compatibility, we can point “-bootclasspath” at the rt.jar of the targeted JRE:
javac -bootclasspath "C:Program FilesJavajdk1.8.0_31jrelibrt.jar" -source 1.8 -target 1.8 <filename>
[Applicable mainly to JDK 8 and lower]
But 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. As an example, let us use “–release” to target Java 8:
javac --release 8 <filename>
There are many IDEs are available for Java development
Let us see how we can overcome “UnsupportedClassVersionError” in Eclipse
Selecting JRE using Java Build Path->Installed JRE
Java compiler level selection :
Java compiler selection
Maven
We can control the version of Java when we build and package a file in Maven. When using Java 7 or older, we set the source and target for the compiler plugin.
Let’s set the source and target using compiler plugin properties:
–release option added in Java 9
Conclusion :
By setting the environment changes either via command-line way or via IDE way or via Maven way, we can overcome “unsupportedclassversion”.
- Cause of the
java.lang.UnsupportedClassVersionError
- Fix the
java.lang.UnsupportedClassVersionError
This tutorial demonstrates the Exception in thread main java.lang.UnsupportedClassVersionError
error in Java.
Cause of the java.lang.UnsupportedClassVersionError
The UnsupportedClassVersionError
is a sub-class of the ClassFormatError
exception, which is thrown when JVM tries to read a class and finds that the class file is malformed or the file cannot be interpreted as a class. Here is the hierarchy of the UnsupportedClassVersionError
exception:
Java.Lang.Object
Java.Lang.Throwable
Java.Lang.Error
Java.Lang.LinkageError
Java.Lang.ClassFormatError
Java.Lang.UnsupportedClassVersionError
The UnsupportedClassVersionError
exception is specific to detecting a class file run by a lower version of Java before and now run by a newer version.
For example, suppose a Java file was run by JDK 12 and now by a newer JRE 8. In that case, it will throw the UnsupportedClassVersionError
exception, or if we compile the class using Java version 1.8 and compile it using Java 1.7, it will throw the same error.
Let’s try an example in a class compiled by the newer version of Java and then run by an older version. See example:
public class Unsupported_Class_Version_Error{
public static void main(String args[]) {
System.out.println("Hello this is Delftstack.com");
}
}
The code above will throw the UnsupportedClassVersionError
as shown below:
Exception in thread "main" java.lang.UnsupportedClassVersionError: Unsupported_Class_Version_Error : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
The output shows the error because the code is compiled at Java 1.8 and executed at Java 1.7. That is why Exception in thread "main" java.lang.UnsupportedClassVersionError: Unsupported_Class_Version_Error : Unsupported major.minor version 52.0
will be thrown.
Fix the java.lang.UnsupportedClassVersionError
There are two conditions for this error, so the solution lies in these two conditions:
- Run the code with the latest JDK and JRE.
- Compile the code with the older version of JDK to match the runtime JDK.
- One simple solution is to use the Java Cross Compilation. If the Production Environment JDK is lower than the Build Environment, we can generate a class file with a lower version using cross-compilation.
The following command will be used to create a class file for the code above:
javac -target 1.7 Unsupported_Class_Version_Error.java
As mentioned above, the solution is to build the application with the same versions at compile and runtime, or at least the compile-time version is lower than the runtime version.
The error above shows the Major Minor versions problem. Major Minor versions are the versions of JRE; for example, for JRE 8, the Major version is 52.0.
We must ensure no Major Minor version problems occur while compiling and executing the Java Class file. The list for the versions of JRE which are compatible with the class are:
Java SE 17 = 61,
Java SE 16 = 60,
Java SE 15 = 59,
Java SE 14 = 58,
Java SE 13 = 57,
Java SE 12 = 56,
Java SE 11 = 55,
Java SE 10 = 54,
Java SE 9 = 53,
Java SE 8 = 52,
Java SE 7 = 51,
Java SE 6.0 = 50,
Java SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45