How to find the java compiler target version from a java class file?

This tutorial gives a nice tip on how to find out the java compiler target which you used to compile out a java binary class. Will it be of great use? I don’t know? Lets have some fun!

Some time back I wrote about java binary class structure and I touched this slightly on that article. I recommend you to go through that article and you will also know about cafebabe and java.

Today one of our lovely reader asked me this question, “how to know the .class is complied by which jdk version?” on facebook and so I wrote this article quickly for him.

In a compiled class (.class file) first thing we see is “ca fe ba be”. This is the java magic number which says that this is a java binary class file. It is immediately followed by minor_version and then major_version.

ClassFile {
  u4 magic;
  u2 minor_version;
  u2 major_version;
  u2 constant_pool_count;
  cp_info constant_pool[constant_pool_count-1];
  u2 access_flags;

Sample hex view of a java binary class:

ca fe ba be 00 00 00 32 ...

So what we have after cafebabe is minor and major version. Do we have directly JDK 1.6 like this version there? No. We have a mapping like the following

  • JDK 1.0 -> major version 45 and minor version 3
  • JDK 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

I searched for these in java specification and couldn’t find any reference to it and so, these versions and mapping are implementation specific to a compiler. I wrote this based on Oracle/Sun JDK.

Ads by Google

One more thing I want to emphasize is we will not be able to find the java compiler verion, that is something like JDK1.60_24 – No! We can find only the target that is 1.0 or 1.1 or 1.x and nothing more than that.

How to find?

JDK has a tool javap, we can use that.

javap -verbose <classfilename>

I wrote a simple java class as follows and compiled it using JDK 1.6

public class TestVersion {
	public static void main(String args[]){
		System.out.println("Test Version");

After compilation,

javap -verbose TestVersion


Compiled from ""
public class TestVersion extends java.lang.Object
  SourceFile: ""
  minor version: 0
  major version: 50
  Constant pool:
const #1 = Method       #6.#15; //  java/lang/Object."<init>":()V
const #2 = Field        #16.#17;        //  java/lang/System.out:Ljava/io/PrintS
const #3 = String       #18;    //  Test Version
const #4 = Method       #19.#20;        //  java/io/PrintStream.println:(Ljava/l

It gives, minor and major version as 0 and 50. Now map this with the list given before, so its JDK 1.6

Source Code/tool

An alternate to javap is using following code we can extract the minor and major version. Compile this class and then use the command

java Version Version.class



public class Version {

	public static void main(String[] args) throws IOException {

	public static String getVersion(String filename) throws IOException {
		String classVersion = "";
		DataInputStream dis = new DataInputStream(new FileInputStream(filename));

		int magic = dis.readInt();
		if (magic != 0xcafebabe) {
			System.out.println(filename + " is not a java class!");
		} else {
			int minor = dis.readUnsignedShort();
			int major = dis.readUnsignedShort();
			classVersion = major + "." + minor;
		return classVersion;

Last week i started my first tutorial on Spring MVC and got good reception from you. Thanks and lot more is coming on Spring!

This Core Java tutorial was added on 22/07/2012.

previous post: Spring MVC Hello World

next post: Dependency Injection (DI) with Spring

55 comments on “How to find the java compiler target version from a java class file?

  1. Hello Sir,

    Thanks for sharing the secreats of java.
    Keep it up.

  2. As u r thinking we r expecting more on Spring and Hibernate stuff. Hope u can do that. Thanks joe.

  3. Nice explanation..Thank u very much.

  4. Hi,

    I am very new person learning java.

    I know only the thing, how to compile and run the java file

    But the command like,
    javap -verbose
    java Version Version.class

    these are new to me and I never forgot by your clean and simple examples.

    Thank you!

  5. Good information about java. Thank you … :)

  6. Hey Joe , as usual like other even this one is the best ! m very eager to read such inner most details of java …. in the entire article most motivating thing was “coming on Spring!” your all future new articles are awaited …. :-)

  7. handy piece of information. thanks

  8. Hi Joe,

    thanks for your article on the JDK/JVM/JRE it really clears the basic.

    many times we don’t know the exact difference between this.

  9. Thanks a ton. Really interesting.

  10. I really like the design of your blog.

  11. joe …….in simple word, u r gr8.

  12. Thanks a lot for sharing this wonderful information..

  13. thanks we were unaware about this query…its very logical query..we never encountered such scenario

  14. Hello Joe,

    I am waiting for the Spring and Hibernate articles.Thanks for your previous articles.

  15. Tried and got

    minor version: 0
    major version: 0


  16. great job !!!
    so thankful to u ….!!!!

  17. great job !!!
    so thankful to u ….!!!!

  18. much worthy article


    Suresh Reddy.B

  19. Good piece of information.Thanks

  20. i gain some info,thank u

  21. Really it is very use full to me…
    thank u…..

    Ram Naresh.A on oct 23rd,2012 11:26 am

  22. You may not need to pass the class filename i.e. “Version.class” – you can programmatically determine that like following way.

    StackTraceElement[] stack = Thread.currentThread ().getStackTrace ();
    StackTraceElement main = stack[stack.length - 1];
    String filename = main.getClassName() + “.class”;


  23. Really sir m very much happy when i read ur article….
    Thank u very much…….keep itup


Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>