2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 import java.io.DataInputStream;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.util.Enumeration;
25 import java.util.Hashtable;
26 import java.util.jar.JarEntry;
27 import java.util.jar.JarInputStream;
29 public class getJavaVersion
32 * Takes a set of Jars and/or class files as arguments. Reports the java
33 * version for the classes
36 public static void main(String[] args) throws IOException
38 Hashtable observed = new Hashtable();
39 for (int i = 0; i < args.length; i++)
41 checkClassVersion(args[i], observed);
43 printVersions(observed, System.out);
46 public static void printVersions(Hashtable observed,
47 java.io.PrintStream outs)
49 if (observed.size() > 0)
53 for (Enumeration keys = observed.keys(); keys.hasMoreElements();)
55 key = (String) keys.nextElement();
66 private static void checkClassVersion(String filename, Hashtable observed)
69 String version = checkClassVersion(filename);
72 // System.err.println("Reading "+filename+" as jar:");
75 JarInputStream jis = new JarInputStream(new FileInputStream(
78 Hashtable perjar = new Hashtable();
79 while ((entry = jis.getNextJarEntry()) != null)
83 if (entry.getName().endsWith(".class"))
87 version = getVersion(new DataInputStream(jis));
90 addVersion(version, observed);
91 addVersion(version, perjar);
100 System.err.println("Jar : " + filename);
101 printVersions(perjar, System.err);
102 } catch (Exception e)
109 addVersion(version, observed);
113 private static void addVersion(String version, Hashtable observed)
117 // System.err.println("Version is '"+version+"'");
118 int[] vrs = (int[]) observed.get(version);
121 vrs = new int[] { 0 };
124 observed.put(version, vrs);
128 private static String checkClassVersion(String filename)
131 DataInputStream in = new DataInputStream(new FileInputStream(filename));
132 return getVersion(in);
135 private static Hashtable versions = null;
137 private static String parseVersions(int minor, int major)
139 if (versions == null)
141 versions = new Hashtable();
142 versions.put("45.3", "1.0.2");
143 versions.put("45.3", "1.1");
144 versions.put("46.0", "1.2");
145 versions.put("47.0", "1.3");
146 versions.put("48.0", "1.4");
147 versions.put("49.0", "1.5");
148 versions.put("50.0", "1.6");
149 versions.put("51.0", "1.7");
150 versions.put("52.0", "1.8");
151 versions.put("53.0", "9");
152 versions.put("54.0", "10");
153 versions.put("55.0", "11");
156 String version = (String) versions.get(major + "." + minor);
159 // get nearest known version
160 version = (String) versions.get(major + ".0");
162 // System.err.println("Version "+version);
165 versions.put(major + "." + minor, "Class v" + major + ".0");
170 private static String getVersion(DataInputStream in) throws IOException
172 int magic = in.readInt();
173 if (magic != 0xcafebabe)
177 int minor = in.readUnsignedShort();
178 int major = in.readUnsignedShort();
179 // System.err.println("Version "+major+"."+minor);
180 return parseVersions(minor, major);