/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package vmversion; import java.io.DataInputStream; import java.io.File; import java.io.IOException; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; /** * * @author tom */ public class Main { private static final int CF_11 = 0x2d; private static final int CF_12 = 0x2e; private static final int CF_13 = 0x2f; private static final int CF_14 = 0x30; private static final int CF_15 = 0x31; private static final int CF_16 = 0x32; /** * @param args the command line arguments */ public static void main(String[] args) { if (args.length == 0) { System.err.println ("usage: VMVersion (file|folder)+"); System.exit (1); } for (String arg : args) { final File file = new File (arg); process (new File[] {file}); } } private static final String getVersion (final short major, final short minor) { switch (major) { case CF_11: return "1.1"; case CF_12: return "1.2"; case CF_13: return "1.3"; case CF_14: return "1.4"; case CF_15: return "1.5"; case CF_16: return "1.6"; default: if (major > CF_16) { return "newer than 1.6"; } else { return "unknown"; } } } private static void process (final ZipFile zf) throws IOException { final Enumeration entries = zf.entries(); ZipEntry entry = null; while (entries.hasMoreElements()) { ZipEntry e = entries.nextElement(); if (e.getName().endsWith(".class")) { entry = e; break; } } if (entry != null) { final DataInputStream in = new DataInputStream(zf.getInputStream(entry)); try { int magic = in.readInt(); short minor = in.readShort(); short major = in.readShort(); if (magic != 0xCAFEBABE) { System.err.println("Invalid class file in: " + zf.getName()); return; } System.out.println(zf.getName()+"\t"+getVersion(major,minor)); } finally { in.close(); } } } private static void process (final File f) throws IOException { assert f != null; try { final ZipFile zf = new ZipFile (f); try { process (zf); } finally { zf.close(); } } catch (ZipException e) { System.err.println("Not a zip file: " + f); } } private static void process (final File[] files) { if (files == null) { return; } for (File f : files) { if (!f.canRead()) { System.err.println("Cannot read file: " + f); continue; } else if (f.isDirectory()) { process (f.listFiles()); } else { try { process (f); } catch (IOException e) { System.err.println("Cannot read file: " + f); } } } } }