How can I read the version of a .class file from within a Java program?
Created May 4, 2012
Chandra Patni According to JVM specification, a class file consists of a single
The following snippet of code can be be used to read the version of a class file.
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(); }