29 September 2011

How to identify J2SE version used to compile JAR FIle

Recently I come to condition when I needed to check the version of J2SE used to compile JAR file.

The following is python code to check the same :)

#!/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 "Your JAR file is compile with JDK 1.1"
elif maj_version==46:
print "Your JAR file is compile with JDK 1.2"
elif maj_version==47:
print Your JAR file is compile with "JDK 1.3"
elif maj_version==48:
print "Your JAR file is compile with JDK 1.4"
elif maj_version==49:
print "Your JAR file is compile with JDK 5.0"
elif maj_version==50:
print "Your JAR file is compile with JDK 6.0"
elif maj_version==60:
print "Your JAR file is compile with JDK 7.0"
break


How to use:
1. Download python from
Python
2. Save above code to testVer.py file into Python installation folder
3. Go to Command Prompt from python folder
4. python testVer.py yourJarFile.jar
this will give you version of jdk used to compile JAR File.

No comments: