Wednesday, 18 May 2022

How to solve UnsupportedClassVersionError in Java?

UnsupportedClassVersionError is 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.

 


This error occur, when you compile the Java class file in higher version (For Ex: java1.8) and try to run on lower version (Ex: Java 1.7)

 

Let’s experiment the same.

 

HelloWorld.java

public class HelloWorld {
	
	public static void main(String[] args) {
		System.out.println("Hello World");
	}

}

 

Let’s compile HelloWorld.java class using Jdk1.8.

$/Library/Java/JavaVirtualMachines/jdk1.8.0_311.jdk/Contents/Home/bin/javac -version
javac 1.8.0_311
$
$/Library/Java/JavaVirtualMachines/jdk1.8.0_311.jdk/Contents/Home/bin/javac HelloWorld.java 

 

Let’s run HelloWorld.class file using Jdk1.7.

$/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/bin/java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
$
$
$/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/bin/java HelloWorld
Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld : 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)

 

Oops, I got the error ‘java.lang.UnsupportedClassVersionError: HelloWorld : Unsupported major.minor version 52.0’.

 

Below table summarizes different major.minor versions of Jdk.

 

JDK version

Major version number

JDK 1.1

45

JDK 1.2

46

JDK 1.3

47

JDK 1.4

48

Java SE 5.0

49

Java SE 6.0

50

Java SE 7

51

Java SE 8

52

Java SE 9

53

Java SE 10

54

Java SE 11

55

Java SE 12

56

Java SE 13

57

Java SE 14

58

Java SE 15

59

Java SE 16

60

Java SE 17

61

 

From the error message, you can see the number 52, that means this class is compiled with Java SE 8 and not compatible with the current running environment.

 

How to resolve this error?

Solution 1: Upgrade Java runtime environment to the compiled version. In our example, we need to upgrade Java run time environment to 1.8.

 

Solution 2: Specify the target and source versions while compiling the class file.

Ex

javac -target 1.7 -source 1.7 HelloWorld.java

$/Library/Java/JavaVirtualMachines/jdk1.8.0_311.jdk/Contents/Home/bin/javac -target 1.7 -source 1.7 HelloWorld.java 
warning: [options] bootstrap class path not set in conjunction with -source 1.7
1 warning
$
$
$/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/bin/java HelloWorld
Hello World

 

References

https://en.wikipedia.org/wiki/Java_class_file

 


You may like

Interview Questions

Get the length of longest row in a two dimensional jagged array in Java

Quick guide to OutOfMemoryError in Java

StackOverFlowError vs OutOfMemoryError in Java

Different ways to iterate over a map using for-each loop

How for-each or enhanced for loop works in Java?

No comments:

Post a Comment