Dear Reader,
I am writing a complete code to figure out what bytecode version a given class was compiled with. Please read carefully, this is a tested code.
Every '.class' file starts of with the following:
* Magic Number [4 bytes]
* Version Information [4 bytes]
A hexdump of a '.class' file reveals:
1.1 ==> CA FE BA BE 00 03 00 2D (Decimal Value means Major Version: 45)
1.2 ==> CA FE BA BE 00 00 00 2E (Decimal Value means Major Version: 46)
1.3 ==> CA FE BA BE 00 00 00 2F (Decimal Value means Major Version: 47)
1.4 ==> CA FE BA BE 00 00 00 30 (Decimal Value means Major Version: 48)
1.5 ==> CA FE BA BE 00 00 00 31 (Decimal Value means Major Version: 49)
1.6 ==> CA FE BA BE 00 00 00 32 (Decimal Value means Major Version: 50)
/*
major minor Java platform version
45 3 1.0
45 3 1.1
46 0 1.2
47 0 1.3
48 0 1.4
49 0 1.5
50 0 1.6
*/
STEPS to see:
1) I have Java installed Version-1.5.
2) I will compile a simple java code using Source 1.2 and create a target file for version 1.2. Remember, You can compile using
any version of Source code, but "target" tag says that your bytecode is ready for that JVM version, in this case 1.2.
3) Run the code to see whether it is compiled successfully to generate the output, however this step is not mandatory.
4) Check or verify what we want to do actually (getting version no of byte code compiled with).
//Java code
//Deepak.java
public class Deepak {
public static void main(String ar[]){
System.out.println("Deepak K. Modi, Expert in java programming");
}
}
//Use command prompt to compile and generate class file (Make sure JAVA_HOME and PATH is set to run java commands).
C:\DOCUME~1\dmodi> javac -source 1.2 -target 1.2 Deepak.java
C:\DOCUME~1\dmodi> java Deepak
Deepak K. Modi, Expert in java programming
After getting output, now you have to run "javap" command to see the version no of compiler using that this
class file "Deepak.class" got generated.
C:\DOCUME~1\dmodi> javap -verbose Deepak
Compiled from "Deepak.java"
public class Deepak extends java.lang.Object
SourceFile: "Deepak.java"
minor version: 0
major version: 46
Constant pool:
const #1 = Method #6.#15; // java/lang/Object."<init>":()V
-----some other contents-------
-----some other contents-------
//That's all, you got the output, see the "Major version" and "Minor Version" displayed above and
compare with the Table given above in the blog for Java Platform Version.
//Testing again using 1.5 version:
C:\DOCUME~1\dmodi> javac -source 1.5 -target 1.5 Deepak.java
C:\DOCUME~1\dmodi> java Deepak
Deepak K. Modi, Expert in java programming
C:\DOCUME~1\dmodi> javap -verbose Deepak
Compiled from "Deepak.java"
public class Deepak extends java.lang.Object
SourceFile: "Deepak.java"
minor version: 0
major version: 49
Constant pool:
const #1 = Method #6.#15; // java/lang/Object."<init>":()V
-----some other contents-------
-----some other contents-------
--------------------------------------END-------------------------------------
Friday, February 4, 2011
Java code to know the compiler version used while generating a Class file
Subscribe to:
Post Comments (Atom)
Nice one(Very helpful)
ReplyDeletei like it bro
ReplyDelete