<p>Do any of you know of a tool that will search for .class files and then display their compiled versions?</p>

<p>I know you can look at them individually in a hex editor but I have a lot of class files to look over (something in my giant application is compiling to Java6 for some reason).</p>

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 51.0 (Java 7), the opening bytes are:
```
```
CA FE BA BE 00 00 00 33
```
```
...where 0xCAFEBABE are the magic bytes, 0x0000 is the minor version and 0x0033 is the major version.
```
```
import java.io.*;

public class Demo {
  public static void main(String[] args) throws IOException {
    ClassLoader loader = Demo.class.getClassLoader();
    try (InputStream in = loader.getResourceAsStream("Demo.class");
        DataInputStream data = new DataInputStream(in)) {
      if (0xCAFEBABE != data.readInt()) {
        throw new IOException("invalid header");
      }
      int minor = data.readUnsignedShort();
      int major = data.readUnsignedShort();
      System.out.println(major + "." + minor);
    }
  }
}
```
```
Walking directories (
File
) and archives (
JarFile
) looking for class files is trivial.
Oracle's
Joe Darcy's blog
lists the
class version to JDK version mappings
up to Java 7:
```
```
Target   Major.minor Hex
1.1      45.3        0x2D
1.2      46.0        0x2E
1.3      47.0        0x2F
1.4      48.0        0x30
5 (1.5)  49.0        0x31
6 (1.6)  50.0        0x32
7 (1.7)  51.0        0x33
8 (1.8)  52.0        0x34
9        53.0        0x35
```
```