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

This Java 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.

[code]javap -verbose <classfilename>[/code]

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,

[code]javap -verbose TestVersion[/code]


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."":()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

[code]java Version Version.class[/code]



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.



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

  1. Muruganalu says:

    Sexy baba sexy !!!!

  2. Anonymous says:

    You rock Joe!!

  3. raja says:


  4. Balaji Seshadri says:


  5. Rishikesh says:

    Nice article Joe!

  6. jagnya says:

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

  7. Kuntal says:

    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”;


  8. Anonymous says:

    Really good articles :)

  9. Ram Naresh says:

    thank u….

  10. Ram Naresh says:

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

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

  11. says:

    i gain some info,thank u

  12. Ravikanth Kovour says:

    Good one to read

  13. Ranjit Kumar Sahu says:

    Good piece of information.Thanks

  14. Anonymous says:

    useless explanation

  15. Anonymous says:

    much worthy article


    Suresh Reddy.B

  16. Venkatesh says:

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

  17. Anonymous says:

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

  18. says:


  19. Jamal says:


    Thanks a lot.

    Best for next,

  20. Anonymous says:

    nice info..thanks :)

  21. Ragupathirajan says:

    Great information. Keep up.

  22. Raks says:

    Good one……

  23. Anonymous says:

    Tried and got

    minor version: 0
    major version: 0


  24. nizam says:

    nice information for me sir

  25. Ritanshu Goel says:

    Really nice blog.thank you sir for help us.

  26. vivek says:

    Hello Joe,

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

  27. rosy says:

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

  28. Hardik says:

    Thanks a lot for sharing this wonderful information..

  29. chandan kumar says:

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

  30. Anonymous says:

    very intresting…thanks

  31. Anonymous says:

    I really like the design of your blog.

  32. suganya says:

    this is very interesting!!!

  33. Arthanarisamy says:

    Thanks a ton. Really interesting.

  34. Mayur Kumar says:

    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.

  35. himanshu sharma says:

    handy piece of information. thanks

  36. GN says:

    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 …. :-)

  37. Dinesh says:

    Good information about java. Thank you … :)

  38. Grace says:


    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!

  39. Rahul Pareek says:

    great sir i m overwhelmed.

  40. khalid says:

    Great job sir

  41. Deepesh Uniyal says:

    Great article….

  42. Rajashekhar says:

    Nice explanation..Thank u very much.

  43. Anonymous says:

    thank you so much!

  44. Anonymous says:

    Very good explanation… Nice..

  45. Sandeep says:

    Good Stuff .. thanks

  46. Pawan says:

    Nice info..thanks..

  47. Ramana says:

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

  48. Pradeep says:

    Good One

  49. Arun says:

    Hello Sir,

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

  50. Kapil S says:

    Nice information

  51. Poomani says:

    Wishes and Thanks joe.. keep it up.

  52. Hiral says:

    Thanks for giving this information!!!!

  53. shams says:

    good anatomy of java class

  54. Anonymous says:

    thank u sir

Your Comment