ASF Bugzilla – Attachment 21822 Details for
Bug 44633
say which jar/class is wrong when throwing UnsupportedClassVersionError
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Lsjar2.java
Lsjar2.java (text/plain), 17.32 KB, created by
Arnaud Jotterand
on 2008-04-15 06:47:29 UTC
(
hide
)
Description:
Lsjar2.java
Filename:
MIME Type:
Creator:
Arnaud Jotterand
Created:
2008-04-15 06:47:29 UTC
Size:
17.32 KB
patch
obsolete
>package com.privasphere.util; >/* ==================================================================== > * Copyright (c) 2002, Hidetoshi Ohuchi <hchacha@users.sourceforge.net> > * All rights reserved. > * > * Redistribution and use in source and binary forms, with or without > * modification, are permitted provided that the following conditions > * are met: > * > * Redistributions of source code must retain the above copyright > * notice, this list of conditions and the following disclaimer. > * > * Redistributions in binary form must reproduce the above copyright > * notice, this list of conditions and the following disclaimer in the > * documentation and/or other materials provided with the distribution. > * > * Neither the name of the hchacha nor the names of its contributors > * may be used to endorse or promote products derived from this > * software without specific prior written permission. > * > * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS > * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT > * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS > * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE > * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, > * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, > * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; > * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER > * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT > * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN > * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE > * POSSIBILITY OF SUCH DAMAGE. > * ==================================================================== > */ > > >import java.io.File; >import java.io.IOException; >import java.util.Enumeration; >import java.util.jar.JarFile; >import java.util.zip.ZipEntry; >import java.util.zip.ZipException; >import java.util.zip.ZipFile; > >import com.sun.org.apache.bcel.internal.classfile.ClassParser; >import com.sun.org.apache.bcel.internal.classfile.Code; >import com.sun.org.apache.bcel.internal.classfile.JavaClass; >import com.sun.org.apache.bcel.internal.classfile.Method; > >/** > * The Main class of 'list a jar file' (lsjar2) . > * > * Copyright (c) 2002, <A HREF="mailto:Hidetoshi Ohuchi > * <hchacha@users.sourceforge.net>"> Hidetoshi Ohuchi > * <hchacha@users.sourceforge.net></A><BR> > * > * @version 0.9.2 - $Id: Lsjar2.java 693 2008-03-24 22:51:00Z privasphere $ > * @author Hidetoshi Ohuchi <hchacha@users.sourceforge.net> > * > * rh: most easily can be executed if you add the line > * > * Main-Class: com.privasphere.util.Lsjar2 > * > * to META-INF/MANIFEST.MF and then do > * > * jar -cvf lsjar.jar lib/bcel-5.2.jar build/com/privasphere/util/Lsjar.class > * > * as per https://issues.apache.org/bugzilla/show_bug.cgi?id=44633 > * > * http://forum.java.sun.com/thread.jspa?threadID=577007&messageID=3791497 > * > */ >public class Lsjar2 { > private static void printVersion() { > System.out.println("lsjar - list a jar file - ver 0.9.2 rh-enhanced"); > } > > private static void printUsage() { > printVersion(); > System.out.println("-- list files in jar file"); > System.out.println("[usage] java -jar lsjar.jar xxxx.jar"); > System.out.println("-- show class file in jar file"); > System.out > .println("[usage] java -jar lsjar.jar {options} dir-yyy xxxx.jar path/file.class"); > System.out.println(" options"); > System.out.println(" -verbose : verbose mode"); > System.out.println(" -all : show results for every class file in jar"); > System.out.println(" -constant : show constant pool"); > System.out.println(" -code : show method code"); > System.out.println(" -jvmVersion : show jvm version"); > System.out > .println(" -dir : analyze all files in a directory"); > } > > public static String DUMMY_JAR = "dummy.jar"; > > public static void main(String[] argv) { > String[] file_name = new String[argv.length]; > int files = 0; > boolean code = false, constants = false, verbose = false, all=false, jvmVersion = false, dir = false; > String name = null; > > // Parse command line arguments. > for (int i = 0; i < argv.length; i++) { > if (argv[i].charAt(0) == '-') { // command line switch > if (argv[i].equals("-constant")) { > constants = true; > } else if (argv[i].equals("-code")) { > code = true; > } else if (argv[i].equals("-jvmVersion")) { > jvmVersion = true; > } else if (argv[i].equals("-dir")) { > dir = true; > } else if (argv[i].equals("-verbose")) { > verbose = true; > } else if (argv[i].equals("-all")) { > all = true; > } else { > System.err.println("Unknown switch " + argv[i] > + " ignored."); > } > } else { // add file name to list > boolean isDir = false; > File dirHere = null; > if (dir) { > dirHere = new File(argv[i]); > if (dirHere.exists()) { > if (dirHere.isDirectory()) { > isDir = true; > } else { > System.err.println(dirHere.getAbsolutePath() > + " is not a directory."); > } > } > } > if (isDir) { > File[] filesArr = dirHere.listFiles(); > if (null == filesArr) { > System.err.println(dirHere.getAbsolutePath() > + " has no content!"); > } else { > if (0 == filesArr.length) { > System.err.println(dirHere.getAbsolutePath() > + " has no content."); > } else { > file_name = new String[filesArr.length]; > for (int j = 0; j < filesArr.length; j++) { > File file = filesArr[j]; > file_name[j] = file.getAbsolutePath(); > files++; > } > } > } > } else { > file_name[files++] = argv[i]; > } > } > } > try { > if (files == 0) { > printUsage(); > } else { > int a = 0; > if (file_name[a].endsWith(".jar")) { > String jarfn = file_name[a]; > files--; > System.arraycopy(file_name, 1, file_name, 0, files); > proc(jarfn, file_name, files, code, constants, verbose, > all, jvmVersion, dir); > } else { > if (dir) { > int j = 0; > for ( j = 0; j < file_name.length; j++) { > String jarfn = file_name[j]; > proc(jarfn, file_name, files, code, constants, verbose, > all, jvmVersion, dir); > } > } else { > if (jvmVersion) { > proc(DUMMY_JAR, file_name, files, code, constants, > verbose, all, jvmVersion, dir); > } else { > System.err.println(file_name[a] > + " does not end with \".jar\"."); > } > > } > > } > } > } catch (IOException e) { > System.err.println(e.getMessage()); > e.printStackTrace(); > } catch (Exception e) { > System.err.println(e); > e.printStackTrace(); > } > } > > private static void proc(String jarfn, String[] file_name, int files, > boolean code, boolean constants, boolean verbose, boolean all, > boolean jvmVersion, boolean dir) throws IOException { > if (files == 0) { > JarFile jar = new JarFile(jarfn); > Enumeration en = jar.entries(); > ClassParser cpr = null; > JavaClass java_class = null; > boolean reportedOnOneJavaFile = false; > while (en.hasMoreElements() && (all || !reportedOnOneJavaFile)) { > try { > ZipEntry et = (ZipEntry) en.nextElement(); > long sz = et.getSize(); > long csz = et.getCompressedSize(); > String etname = et.getName(); > //printEntryLine(sz, csz, etname); > try { > cpr = new ClassParser(jarfn, etname); > if (etname.endsWith(".class")) { > java_class = cpr.parse(); > } > } catch (IOException e) { > System.err.println(etname + " " + e.getMessage()); > continue; > } catch (Exception ex) { > System.out.println("can not find " + etname + " in " > + jarfn + "."); > return; > } > report(jarfn, constants, code, verbose, all, cpr, etname, sz, > csz, java_class, jvmVersion); > } catch (ZipException e) { > if ("Access is denied".equalsIgnoreCase(e.getMessage())) { > System.err.println(jarfn + ": ZipException " > + e.getMessage()); > continue; > } > System.err.println(jarfn + ": ZipException " > + e.getMessage()); > throw e; > } > } > return; > } > > String name; > String jarfnHere = jarfn; > JavaClass java_class = null; > for (int i = 0; i < files; i++) { > String entname = null; > long entsize = 0; > long entcsz = 0; > ClassParser cpr = null; > > name = file_name[i]; > if (dir) { > jarfnHere = name; > } > if (jarfnHere != null && !DUMMY_JAR.equalsIgnoreCase(jarfnHere)) { > if (!new File(jarfnHere).exists()) { > System.out.println("can not find " + jarfnHere); > continue; > } > try { > ZipFile zip = new ZipFile(jarfnHere); > ZipEntry entry = null; > boolean reportedOnOneJavaFile = false; > Enumeration en = zip.entries(); > if (!en.hasMoreElements()) { > System.out.println(jarfnHere + " has no content"); > } else { > while (en.hasMoreElements() && (all || !reportedOnOneJavaFile)) { > if (dir) { > entry = (ZipEntry) en.nextElement(); > name = entry.getName(); > } else { > entry = zip.getEntry(name); > reportedOnOneJavaFile = true; > } > entcsz = entry.getCompressedSize(); > entsize = entry.getSize(); > entname = entry.getName(); > try { > cpr = new ClassParser(jarfnHere, name); > if (entname.endsWith(".class")) { > java_class = cpr.parse(); > reportedOnOneJavaFile = true; > if (all) { > report(jarfnHere, constants, code, verbose, all, cpr, entname, > entsize, entcsz, java_class, jvmVersion); > } > } else if (dir) { > continue; > } > } catch (Exception ex) { > System.out.println("can not found " + name > + " in " + jarfnHere); > continue; > } > } > } > } catch (ZipException e) { > if ("Access is denied".equalsIgnoreCase(e.getMessage())) { > System.err.println(jarfnHere + ": ZipException " > + e.getMessage()); > continue; > } else if ("error in opening zip file".equalsIgnoreCase(e > .getMessage())) { > System.err.println(jarfnHere + ": ZipException " > + e.getMessage()); > continue; > } else if ("Is a directory" > .equalsIgnoreCase(e.getMessage())) { > System.err.println(jarfnHere + ": ZipException " > + e.getMessage()); > continue; > } > > System.err.println(jarfnHere + ": ZipException " > + e.getMessage()); > throw e; > } > } else { > if (!new File(name).exists()) { > System.out.println("can not find " + name); > continue; > } > cpr = new ClassParser(name); > if (cpr == null) { > System.out.println("can not instantiate ClassParser for file " + name); > continue; > } > java_class = cpr.parse(); > if (null == entname) { > entname = name; > } > } > report(jarfnHere, constants, code, verbose, all, cpr, entname, entsize, > entcsz, java_class, jvmVersion); > } > } > > private static boolean report(String jarfn, boolean constants, > boolean code, boolean verbose, boolean all, ClassParser cpr, String entname, > long entsize, long entcsz, JavaClass java_class, boolean jvmVersion) > throws ClassFormatError, IOException { > boolean reportedOnOneJavaFile = false; > // JavaClass java_class = cpr.parse(); > > if (entname != null && !jvmVersion) { > printEntryLine(entsize, entcsz, entname); > } > if (null != java_class) { > if (verbose) { > System.out.println(java_class); // Dump the contents > } > String fileName = jarfn; > if (DUMMY_JAR.equalsIgnoreCase(jarfn)) { > fileName = entname; > } > String fileNameInfo = ""; > if (all) { > fileNameInfo = ", File Name: " + java_class.getFileName(); > } > System.out.println(fileName + " Major: " + java_class.getMajor() > + ", Minor: " + java_class.getMinor() > + fileNameInfo); > reportedOnOneJavaFile = true; > if (constants) { // Dump the constant pool ? > System.out.println(java_class.getConstantPool()); > } > > if (code) { // Dump the method code ? > printCode(java_class.getMethods(), verbose); > } > } > return reportedOnOneJavaFile; > } > > private static void printEntryLine(long sz, long csz, String name) { > int cmp = 0; > > if (sz != 0) { > cmp = (int) ((100L * csz) / sz); > } > > String s = Long.toString(sz); > for (int i = s.length(); i < 8; i++) { > System.out.print(' '); > } > System.out.print(sz); > System.out.print(' '); > > String cs = Integer.toString(cmp); > for (int i = cs.length(); i < 3; i++) { > System.out.print(' '); > } > System.out.print(cs); > System.out.print('%'); > System.out.print(' '); > > System.out.println(name); > } > > // Dump the disassembled code of all methods in the class. > private static void printCode(Method[] methods, boolean verbose) { > for (int i = 0; i < methods.length; i++) { > // print a method name. : ex. "public static void main(String > // argv[])" > System.out.println(methods[i]); > > Code code = methods[i].getCode(); > if (code != null) { > System.out.println(code.toString(verbose)); > } > } > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 44633
:
21704
| 21822