2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 import java.util.jar.JarEntry;
22 import java.util.jar.JarInputStream;
24 public class getJavaVersion
27 * Takes a set of Jars and/or class files as arguments. Reports the java version for the classes
30 public static void main(String[] args) throws IOException
32 Hashtable observed=new Hashtable();
33 for (int i = 0; i < args.length; i++)
35 checkClassVersion(args[i], observed);
37 printVersions(observed, System.out);
39 public static void printVersions(Hashtable observed, java.io.PrintStream outs)
41 if (observed.size()>0)
45 for (Enumeration keys = observed.keys(); keys.hasMoreElements(); ) {
46 key = (String) keys.nextElement();
57 private static void checkClassVersion(String filename, Hashtable observed)
60 String version = checkClassVersion(filename);
63 // System.err.println("Reading "+filename+" as jar:");
66 JarInputStream jis = new JarInputStream(new FileInputStream(
69 Hashtable perjar=new Hashtable();
70 while ((entry = jis.getNextJarEntry()) != null)
74 if (entry.getName().endsWith(".class"))
78 version = getVersion(new DataInputStream(jis));
81 addVersion(version, observed);
82 addVersion(version, perjar);
91 System.err.println("Jar : "+filename);
92 printVersions(perjar,System.err);
100 addVersion(version, observed);
104 private static void addVersion(String version, Hashtable observed)
108 // System.err.println("Version is '"+version+"'");
109 int[] vrs = (int[]) observed.get(version);
116 observed.put(version, vrs);
120 private static String checkClassVersion(String filename)
123 DataInputStream in = new DataInputStream(new FileInputStream(filename));
124 return getVersion(in);
127 private static Hashtable versions = null;
129 private static String parseVersions(int minor, int major)
131 if (versions == null)
133 versions = new Hashtable();
134 versions.put("45.3", "1.0");
135 versions.put("45.3", "1.1");
136 versions.put("46.0", "1.2");
137 versions.put("47.0", "1.3");
138 versions.put("48.0", "1.4");
139 versions.put("49.0", "1.5");
140 versions.put("50.0", "1.6");
142 String version = (String) versions.get(major + "."
146 // get nearest known version
147 version = (String) versions.get(major + ".0");
149 // System.err.println("Version "+version);
153 private static String getVersion(DataInputStream in) throws IOException
155 int magic = in.readInt();
156 if (magic != 0xcafebabe)
160 int minor = in.readUnsignedShort();
161 int major = in.readUnsignedShort();
162 // System.err.println("Version "+major+"."+minor);
163 return parseVersions(minor, major);