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

Last modified on August 1st, 2014 by Joe.

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

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.

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!

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

  1. Anonymous says:

    thank u sir

  2. shams says:

    good anatomy of java class

  3. Hiral says:

    Thanks for giving this information!!!!

  4. Poomani says:

    Wishes and Thanks joe.. keep it up.

  5. Kapil S says:

    Nice information

  6. Arun says:

    Hello Sir,

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

  7. Pradeep says:

    Good One

  8. Ramana says:

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

  9. Pawan says:

    Nice info..thanks..

  10. Sandeep says:

    Good Stuff .. thanks

  11. Anonymous says:

    Very good explanation… Nice..

  12. Anonymous says:

    thank you so much!

  13. Rajashekhar says:

    Nice explanation..Thank u very much.

  14. Deepesh Uniyal says:

    Great article….

  15. khalid says:

    Great job sir

  16. Rahul Pareek says:

    great sir i m overwhelmed.

  17. 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!

  18. Dinesh says:

    Good information about java. Thank you … :)

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

  20. himanshu sharma says:

    handy piece of information. thanks

  21. 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.

  22. Arthanarisamy says:

    Thanks a ton. Really interesting.

  23. suganya says:

    this is very interesting!!!

  24. Anonymous says:

    I really like the design of your blog.

  25. Anonymous says:

    very intresting…thanks

  26. chandan kumar says:

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

  27. Hardik says:

    Thanks a lot for sharing this wonderful information..

  28. rosy says:

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

  29. vivek says:

    Hello Joe,

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

  30. Ritanshu Goel says:

    Really nice blog.thank you sir for help us.

  31. nizam says:

    nice information for me sir

  32. Garcia says:

    Tried and got

    minor version: 0
    major version: 0


  33. Raks says:

    Good one……

  34. Ragupathirajan says:

    Great information. Keep up.

  35. Anonymous says:

    nice info..thanks :)

  36. Jamal says:


    Thanks a lot.

    Best for next,

  37. says:


  38. Anonymous says:

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

  39. Venkatesh says:

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

  40. Anonymous says:

    much worthy article


    Suresh Reddy.B

  41. Anonymous says:

    useless explanation

  42. Ranjit Kumar Sahu says:

    Good piece of information.Thanks

  43. Ravikanth Kovour says:

    Good one to read

  44. says:

    i gain some info,thank u

  45. Ram Naresh says:

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

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

  46. Ram Naresh says:

    thank u….

  47. Anonymous says:

    Really good articles :)

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


  49. jagnya says:

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

  50. Rishikesh says:

    Nice article Joe!

  51. Balaji Seshadri says:


  52. satya says:

    thaks sir

  53. raja says:


  54. Anonymous says:

    You rock Joe!!

  55. Muruganalu says:

    Sexy baba sexy !!!!

Comments are closed for "How to find the java compiler target version from a java class file?".