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 "TestVersion.java"
public class TestVersion extends java.lang.Object
  SourceFile: "TestVersion.java"
  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
tream;
const #3 = String       #18;    //  Test Version
const #4 = Method       #19.#20;        //  java/io/PrintStream.println:(Ljava/l
ang/String;)V...
...

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]

 

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class Version {

	public static void main(String[] args) throws IOException {
		System.out.println(getVersion(args[0]));
	}

	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;
		}
		dis.close();
		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. Anonymous

    thank u sir

  2. shams

    good anatomy of java class

  3. Hiral

    Thanks for giving this information!!!!

  4. Poomani

    Wishes and Thanks joe.. keep it up.

  5. Kapil S

    Nice information

  6. Arun

    Hello Sir,

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

  7. Pradeep

    Good One
    Thanks!!

  8. Ramana

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

  9. Pawan

    Nice info..thanks..

  10. Sandeep

    Good Stuff .. thanks

  11. Anonymous

    Very good explanation… Nice..

  12. Anonymous

    thank you so much!

  13. Rajashekhar

    Nice explanation..Thank u very much.

  14. Deepesh Uniyal

    Great article….

  15. khalid

    Great job sir

  16. Rahul Pareek

    great sir i m overwhelmed.

  17. Grace

    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!

  18. Dinesh

    Good information about java. Thank you … :)

  19. GN

    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

    handy piece of information. thanks

  21. Mayur Kumar

    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

    Thanks a ton. Really interesting.

  23. suganya

    this is very interesting!!!

  24. Anonymous

    I really like the design of your blog.

  25. Anonymous

    very intresting…thanks

  26. chandan kumar

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

  27. Hardik

    Thanks a lot for sharing this wonderful information..

  28. rosy

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

  29. vivek

    Hello Joe,

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

  30. Really nice blog.thank you sir for help us.

  31. nizam

    nice information for me sir

  32. Anonymous

    Tried and got

    minor version: 0
    major version: 0

    means?

  33. Raks

    Good one……

  34. Ragupathirajan

    Great information. Keep up.

  35. Anonymous

    nice info..thanks :)

  36. Jamal

    Hi,

    Thanks a lot.

    Best for next,

  37. Anonymous

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

  38. Venkatesh

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

  39. Anonymous

    much worthy article

    Thanks

    Suresh Reddy.B

  40. Anonymous

    useless explanation

  41. Ranjit Kumar Sahu

    Good piece of information.Thanks

  42. Ravikanth Kovour

    Good one to read

  43. tejaswini.ch

    i gain some info,thank u
    thejaswini.ch

  44. Anonymous

    wooooooowwwwwww thanks

  45. Ram Naresh

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

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

  46. Ram Naresh

    thank u….

  47. Anonymous

    Really good articles :)

  48. Kuntal

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

    Cheers,

  49. jagnya

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

  50. Rishikesh

    Nice article Joe!

  51. Balaji Seshadri

    YOU HAVE NOT MENTIONED HOW TO FIND OUT THE EXACT VERSION, CAN U WRITE ONE….LIKE IF MY JDK IS 1.6.035 HOW TO FIND THAT, (WRITTEN IN BOLD LETTERS SO THAT YOU CAN IDENTIFY MINE….ITS VERY URGENT PLS HELP)

  52. Anonymous

    You rock Joe!!

Your Comment