This tutorial provides a great way to find Java compiler version for a class file generated. It is one of the common problem among Java developers to know the exact version in which the JAR file is created. If you receive a JAR file, can you find what is the target compiler version for that JAR file to be deployed?. If developer not aware of the details, it is difficult for them to test the compatibility issues. If the run the code with different version, runtime exception will be thrown. Before start reading this article, If you want to understand the basics of Java compiler and JVM architecture, please go through the articles Java Compiler API, JVM, JRE and JDK difference and JVM.
java.lang.UnsupportedClassVersionError: test class : Unsupported major.minor version 51.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(Unknown Source)
Without knowing the Java version in which the JAR is created is difficult for the developers. There is no way to check the version in the JAR file itself. We can check the version details only using the class files inside the JAR file. Unzip the JAR and take out any one class file to be verified.
- JDK 1.0 — major version 45 and minor version 3
- DK 1.1 — major version 45 and minor version 3
- JDK 1.2 — major version 46 and minor version 0
- JDK 1.3 — major version 47 and minor version 0
- JDK 1.4 — major version 48 and minor version 0
- JDK 1.5 — major version 49 and minor version 0
- JDK 1.6 — major version 50 and minor version 0
- JDK 1.7 — major version 51 and minor version 0
These version could be different for each implementation of the compiler. The above details are specific to Oracle/Sun JDK. JDK has javap command to find the version details.
javap -verbose
You will find the following details displayed on the screen.
javapThere is simple program which can get you the Java version details. Run the below program by passing the class file name as the arguments, this would return you the major version details.
package com; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; public class GetVersion { public static void main(String[] args) throws IOException { System.out.println(getVersionDetails(args[0])); } public static String getVersionDetails(String filename) throws IOException { String version = ""; DataInputStream stream = new DataInputStream(new FileInputStream(filename)); int magicBytes = stream.readInt(); if (magicBytes != 0xcafebabe) { System.out.println(filename + " is not a valid java file!"); } else { int minorVersion = stream.readUnsignedShort(); int majorVersion = stream.readUnsignedShort(); version = majorVersion + "." + minorVersion; } stream.close(); return version; } }
javap command returns the complete details of the bytecode. In that there is major version and minor version which is for identifying the Java version details. I hope this tutorial have provided nice tip on finding the version details for the Java class. If you have any more tips, please contact us, we will publish it in our website.
Reference Books:
- Java: The Complete Reference
- Head First Java by Kathy Sierra, Bert Bates