Deducing the JDK Version of a .jar File

Here is a little script that uses Pyton to examine the contents of a jar file (or specifically the first .class file it comes across) and then reads the major version byte and maps it to a JDk version. May be useful if you have a bunch of jars compiled by different JDKs and want to figure out which is which.

[sourcecode language=”python”]#!/usr/bin/python

import zipfile
import sys
import re

class_pattern=re.compile("/?\w*.class$")

for arg in sys.argv[1:]:
print ‘%s:’ % arg,
file=zipfile.ZipFile(arg, "r")

for entry in file.filelist:
if class_pattern.search(entry.filename):
bytes = file.read(entry.filename)
maj_version=ord(bytes[7])
if maj_version==45:
print "JDK 1.1"
elif maj_version==46:
print "JDK 1.2"
elif maj_version==47:
print "JDK 1.3"
elif maj_version==48:
print "JDK 1.4"
elif maj_version==49:
print "JDK 5.0"
elif maj_version==50:
print "JDK 6.0"
break
[/sourcecode]