/* * JBoss, Home of Professional Open Source * Copyright 2005, JBoss Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ import java.io.FileInputStream; import java.util.jar.Attributes; import java.util.jar.JarFile; import java.util.jar.Manifest; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; /** A little utility that displays the contents of composite jars * * @author Scott.Stark@jboss.org */ public class ListJar { static String[] jarSuffixes = { ".jar", ".zip", ".ear", ".war", ".sar" }; static String jarPrefix = "+- "; static String entryPrefix = "| "; static byte[] buffer = new byte[65535]; static boolean showOnlyArchives = false; static { for(int i = 0; i < 10; i ++) { jarPrefix += "+- "; entryPrefix += "| "; } } static boolean isJar(String name) { boolean isJar = false; for(int s = 0; isJar == false && s < jarSuffixes.length; s ++) { String suffix = jarSuffixes[s]; isJar |= name.endsWith(suffix); } return isJar; } static void processEntry(ZipInputStream zis, ZipEntry entry, int level) throws Exception { String name = entry.getName(); boolean isDirectory = entry.isDirectory(); if( isDirectory == true ) return; // See if this is a jar archive if( isJar(name) ) { System.out.print(jarPrefix.substring(0, 3*level)); System.out.println(name+" (archive)"); try { ZipInputStream entryZIS = new ZipInputStream(zis); processJar(entryZIS, ++ level); } catch(Exception e) { e.printStackTrace(); } } else if( name.endsWith("MANIFEST.MF") ) { System.out.print(entryPrefix.substring(0, 3*(level-1))); System.out.print("+- "); System.out.print(name); Manifest mf = new Manifest(zis); Attributes main = mf.getMainAttributes(); String cp = main.getValue(Attributes.Name.CLASS_PATH); if( cp != null ) { System.out.print(" Class-Path: "); System.out.print(cp); System.out.print(' '); } System.out.println(); } else if( showOnlyArchives == false ) { System.out.print(entryPrefix.substring(0, 3*(level-1))); System.out.print("+- "); System.out.println(name); } } static void processJar(ZipInputStream zis, int level) throws Exception { ZipEntry entry; while( (entry = zis.getNextEntry()) != null ) { processEntry(zis, entry, level); } } /** List the jar contents * @param args the command line arguments */ public static void main(String[] args) throws Exception { String name = args[0]; JarFile jarFile = new JarFile(name); Manifest mf = jarFile.getManifest(); FileInputStream fis = new FileInputStream(name); ZipInputStream zis = new ZipInputStream(fis); System.out.println(name); processJar(zis, 1); System.out.println("Done"); } }