How can I read the version of a .class file from within a Java program?

Chandra Patni

According to JVM specification, a class file consists of a single ClassFile structure (See JVM specification §4.1).
The following snippet of code can be be used to read the version of a class file.

try {
    RandomAccessFile raf = new RandomAccessFile("Question3.class", "r");
    raf.skipBytes(4);
    int minor, major;
    System.out.println("Minor Version = " + (minor = raf.readUnsignedShort()));
    System.out.println("Major Version = " + (major = raf.readUnsignedShort()));
    System.out.println("Class file Version = " + major + "." + minor);
}
catch (IOException ioe) {
    ioe.printStackTrace();
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact