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.
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
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!
Comments are closed for "How to find the java compiler target version from a java class file?".
thank u sir
good anatomy of java class
Thanks for giving this information!!!!
Wishes and Thanks joe.. keep it up.
Nice information
Hello Sir,
Thanks for sharing the secreats of java.
Keep it up.
Good One
Thanks!!
As u r thinking we r expecting more on Spring and Hibernate stuff. Hope u can do that. Thanks joe.
Nice info..thanks..
Good Stuff .. thanks
Very good explanation… Nice..
thank you so much!
Nice explanation..Thank u very much.
Great article….
Great job sir
great sir i m overwhelmed.
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!
Good information about java. Thank you … :)
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 …. :-)
handy piece of information. thanks
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.
Thanks a ton. Really interesting.
this is very interesting!!!
I really like the design of your blog.
very intresting…thanks
joe …….in simple word, u r gr8.
Thanks a lot for sharing this wonderful information..
thanks we were unaware about this query…its very logical query..we never encountered such scenario
Hello Joe,
I am waiting for the Spring and Hibernate articles.Thanks for your previous articles.
Really nice blog.thank you sir for help us.
nice information for me sir
Tried and got
minor version: 0
major version: 0
means?
Good one……
Great information. Keep up.
nice info..thanks :)
Hi,
Thanks a lot.
Best for next,
great!!!!!!!!!
great job !!!
so thankful to u ….!!!!
great job !!!
so thankful to u ….!!!!
much worthy article
Thanks
Suresh Reddy.B
useless explanation
Good piece of information.Thanks
Good one to read
i gain some info,thank u
thejaswini.ch
Really it is very use full to me…
thank u…..
Ram Naresh.A on oct 23rd,2012 11:26 am
thank u….
Really good articles :)
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,
Really sir m very much happy when i read ur article….
Thank u very much…….keep itup
Nice article Joe!
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)
thaks sir
good
You rock Joe!!
Sexy baba sexy !!!!