way to check the Java class file is compiled into which version

Hi All,

In this article I will show how to find the compiler version information of a java class file. It is very easy to find this information using java command.

Java virtual machine class file format.
Each class file contains the definition of a single class or interface. Although a class or interface need not have an external representation literally contained in a file (for instance, because the class is generated by a class loader), we will colloquially refer to any valid representation of a class or interface as being in the class file format.

A class file consists of a stream of 8-bit bytes. All 16-bit, 32-bit, and 64-bit quantities are constructed by reading in two, four, and eight consecutive 8-bit bytes, respectively. Multibyte data items are always stored in big-endian order, where the high bytes come first. In the Java and Java 2 platforms, this format is supported by interfaces java.io.DataInput and java.io.DataOutput and classes such as java.io.DataInputStream and java.io.DataOutputStream.

A class file consists of a single ClassFile structure:

    ClassFile {
    	u4 magic;
    	u2 minor_version;
    	u2 major_version;
    	u2 constant_pool_count;
    	cp_info constant_pool[constant_pool_count-1];
    	u2 access_flags;
    	u2 this_class;
    	u2 super_class;
    	u2 interfaces_count;
    	u2 interfaces[interfaces_count];
    	u2 fields_count;
    	field_info fields[fields_count];
    	u2 methods_count;
    	method_info methods[methods_count];
    	u2 attributes_count;
    	attribute_info attributes[attributes_count];
    }

For example you want to inspect is TempJava.clas. Open the command console move to the compile class location. And run below command.

javap -verbose TempClass

And below is the example output:

Compiled from “TempClass.java”
public class TempClass extends java.lang.Object
SourceFile: “TempClass.java”
minor version: 0
major version: 50
Constant pool:
const #1 = class #2; // TempClass
const #2 = Asciz TempClass;
const #3 = class #4; // java/lang/Object
const #4 = Asciz java/lang/Object;

The first 4 bytes are a magic number, 0xCAFEBABe, to identify a valid class file then the next 2 bytes identify the class format version (major and minor).

Possible major/minor value :
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

About javap.exe:
Its Sun’s simple disassembler.

It works much like java.exe except instead of running the program it takes the class file apart and shows you how it works at the byte code level, e. g.rem Disassemble a class file

where options include:

-c                                                                          Disassemble the code

-classpath <pathlist>                                   Specify where to find user class files

-extdirs <dirs>                                                 Override location of installed extensions

-help                                                                   Print this usage message

-J<flag>                                                              Pass <flag> directly to the runtime system

-l                                                                           Print line number and local variable tables

-public                                                                Show only public classes and members

-protected                                                         Show protected/public classes and members

-package                                                           Show package/protected/public classes    and members (default)

-private                                                              Show all classes and members

-s                                                                          Print internal type signatures

-bootclasspath <pathlist>                         Override location of class files loaded  by the bootstrap class loader

-verbose                                                           Print stack size, number of locals and args for methods, If verifying, print reasons for failure



javap.exe -classpath E:\;. -c -l -private -s -verbose Biorhythms > bytecodes.txt

[Source] http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#80959


Thanks
R Vashi

So it will be good if there is a way to check the Java class file is compiled into which version.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s