You could load them with the BCEL.
JavaClass javaClass = Repository.lookupClass("foo.Bar");
System.out.println(javaClass.getMajor() + "." + javaClass.getMinor());
The class JavaClass has getMajor() and getMinor() methods for inspecting the class version.
That said, it is easy enough to read the class file signature and get these values without a 3rd party API. All you need to do is read the first 8 bytes.
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
For class file version 50.0 (Java 1.6), the opening bytes are:
CA FE BA BE 00 00 00 32
...where 0xCAFEBABE are the magic bytes, 0x0000 is the minor version and 0x0032 is the major version.
public class Demo {
private static int toInt(byte upper, byte lower) {
int ret = upper << 8;
ret |= lower;
return ret;
}
public static void main(String[] args) throws Exception {
ClassLoader loader = Demo.class.getClassLoader();
InputStream in = loader.getResourceAsStream("Demo.class");
try {
byte[] buffer = new byte[8];
in.read(buffer);
//TODO: error checking
int minor = toInt(buffer[4], buffer[5]);
int major = toInt(buffer[6], buffer[7]);
System.out.println(major+"."+minor);
} finally {
in.close();
}
}
}
Walking directories (File) and archives (JarFile) looking for class files is trivial.