Thursday, August 4, 2022

How to fix java.lang.UnsupportedClassVersionError: Bad version number in .class files? Example

How to fix Bad version number in the .class file
"java.lang.UnsupportedClassVersionError: Bad version number in .class file" is a common error in Java programming language which comes when you try to run a Java class file which was compiled by different version of Java compiler than then JRE version you are using to run it. In our last article, we discussed that how to resolve Java.lang.UnSupportedClassVersionError and found that it comes when a major and minor version of the class is not supported by Java virtual machine or JRE running the program. Though "java.lang.UnsupportedClassVersionError: Bad version number in .class file" is a little different than that of its manifestation and Cause, both are related to the mismatch in compiler javac and runtime java version. 


The UnsupportedClassVersionError is not as difficult as Java.lang.OutOfMemoryError and neither its solution is too complex but what is hard is thinking in the right direction because the cause of different types of UnsupportedClassVersionError is different.


Cause of java.lang.UnsupportedClassVersionError: Bad version number in .class file

"java.lang.UnsupportedClassVersionError: Bad version number in .class file" comes when you compile a Java class in higher version of Java Compiler and run it on a lower version of Java virtual machine or JRE. For example, if you have compiled your Java class using Java SE 17 compiler and then you try to run it on Java SE 11 or Java SE 8 then you will get this error.

Similarly, you can get the Bad Version number in class file error if you compile your Java source file by Java SE 11 compiler, which comes when you install Java 11 JDK in your machine and then you try to run it but there is a lower version of JRE is configured in PATH or it comes first in the PATH if you have multiple JRE installed in your machine. 

The key here is you need to run your Java program or class file in the version greater than or equal to the version which is used to compile your Java program. Java is backward compatible which means a JAR file created by Java 1 will still work in Java SE 17 but a JAR File created by Java SE 17 cannot work in Java 1 as it doesn't have all the new enhancements. 

It's more of a common sense than any rocket science but just finding which version of Java is used to compile and run the program can take time and requires experienced to troubleshoot and think in the right direction. 




java.lang.UnsupportedClassVersionError: Bad version number in .class file

To understand this UnsupportedClassVersionError better let's reproduce it via a simple example in Java:

1) Create Loan.java and compile it with JDK 1.6 or Java SE 17

2) Run Loan.class with JRE 1.5 or any version lower than Java 17 like Java 8 or Java 11. I have used Java 5 here

C:\Program Files\Java\jre1.5.0_06\bin>java -version
java version "1.5.0_06"

C:\Program Files\Java\jre1.5.0_06\bin>java Loan
java.lang.UnsupportedClassVersionError: Bad version number in .class file
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)


How to fix java.lang.UnsupportedClassVersionError: Bad version number in .class files? Example

Solution:

java.lang.UnsupportedClassVersionError: Bad version number in .class fileNow you know that your source is compiled for a higher version of JRE or Java runtime if it doesn't work in JDK 1.5 then try to run on JDK 1.6 and you will be able to remove "Bad version number in .class file". The same problem can come to any other Java version for example, a class file compiled by JDK 11 will not run on Java 8 or Java 9 JRE. Similarly, a class file compiled by Java 17 compiler will not run on Java SE 11 JRE. 


Java Tutorial you may like:

No comments:

Post a Comment