Java class version

Time to time it might happen that you need to know which version the class files were compiled for. Or to be more specific what target were specified while running javac compiler. As target specifies VM version the classes were generated for. This can be specified in maven as follows:

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<target>1.6</target>
</configuration>
</plugin>

It is not a rocket science, right. To find out the version the code were generated for we use javap (java class file disassembler). The following line do the trick:

javap -verbose -classpath versiontest-1.0.jar cz.test.string.StringPlaying

Compiled from "StringPlaying.java"
public class cz.test.string.StringPlaying extends java.lang.Object
SourceFile: "StringPlaying.java"
minor version: 0
major version: 50
Constant pool:
const #1 = Method       #12.#28;        //  java/lang/Object."<init>":()V
const #2 = String       #29;            //  beekeeper
const #3 = Method       #30.#31;        //  java/lang/String.substring:(II)Ljava/lang/String;

Major version matches java version based on following table


Table taken from Oracle blog

3 thoughts on “Java class version

  1. An impressive share! I’ve just forwarded this onto a co-worker who was conducting
    a little research on this. And he actually bought me dinner due to the fact that I discovered it for him…
    lol. So allow me to reword this…. Thanks for the
    meal!! But yeah, thanx for spending some time to
    talk about this subject here on your site.

    Liked by 1 person

  2. Pingback: Structure of Java Virtual Machine (JVM) | jaksky

Leave a reply to weight Loss pill Cancel reply