When we compile our Java programs, each resulting .class file contains a major version number. For example, if we compile our code with Java 17 (major version 61), and then try to run the class file on Java 16 or earlier, we will encounter the error: Unsupported class file major version 61. This situation often happens when our development environment and production server are not aligned.
1. How to Identify the Major Version of a Java Class File
Every compiled Java class file includes a major version number that indicates which version of Java was used for compilation. For example:
- Java 17 produces class files with major version 61
- Java 11 produces class files with major version 55
- Java 8 produces class files with major version 52
Below is a quick reference table:
| Java SE Version | Major Version | Hexadecimal |
|---|---|---|
| 17 | 61 | 0x3D |
| 11 | 55 | 0x37 |
| 9 | 53 | 0x35 |
| 8 | 52 | 0x34 |
We can think of this as a way to quickly verify the compatibility of our compiled code with different Java runtime environments.
Real-World Example:
Imagine we are developing a web application using Java 17. Our team compiles the application on our local machines, but our production server is still using Java 11. When we deploy the application, the server will throw an error about an unsupported class file version because the server’s Java version does not support major version 61.
For a complete list of major versions for Java class files, please review this table.
2. Dealing with the “Unsupported Class File Major Version XX” Error
This error typically occurs when there is a mismatch between the Java version used for compiling the code and the one used for running it. Consider these scenarios:
-
Scenario 1:
We use Maven with Java 11 (major version 55) to package our JAR file, but then deploy the application on a production server running Java 8. In this case, we will see the error: Unsupported class file major version 55. -
Scenario 2:
We use Gradle with Java 8 (major version 52) to compile our code, but our production server is still on Java 1.4. Here, the error would be: Unsupported class file major version 52.
In our development practices, we must ensure that both our compile-time and runtime environments are consistent. Tools like IntelliJ IDEA, Eclipse, JAVA_HOME PATH, Maven, and Gradle might be configured with different JDKs, so we should always verify that our development and production environments are using compatible versions.
Real-World Example:
Suppose our team uses IntelliJ IDEA to compile our application with Java 17 for its new features, but the production server hasn’t been updated yet and still runs Java 11. When we deploy our updated application, we immediately face the "Unsupported class file major version 61" error. To resolve this, we either need to update our server’s Java version or compile our code with a compatible version.
3. Conclusion
By understanding the major version numbers embedded in Java class files, we can avoid compatibility issues between our development and production environments. We recommend always verifying the Java version used during both the compilation and runtime phases to ensure smooth deployments.
I’m lost. I am just a user and the latest download of the JRE is 8. So how can a developer develop something that is a later version? I see Java up to version 20. Very confusing. When I go to a command prompt and look at a version (or the configure java app), I see : java version “1.8.0_351”
Java(TM) SE Runtime Environment (build 1.8.0_351-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.351-b10, mixed mode) I’m lost.
Since java 8, the JRE and JDK are packaged together and cannot be downloaded separately (through the official channels at least). That is why the latest JRE that you can find is 8.
There’s multiple java (runtime / langauge / standard library / vendor) versions being supported concurrently similar to how windows 10 and windows 11 continue to be sold and receive updates. On top of that, there’s also some licensing issues with later versions of java as (partially) covered in this wikipedia article: https://en.wikipedia.org/wiki/Java_version_history