Search all jar and zip files in the current directory for a given class file : Jar File « File Input Output « Java






Search all jar and zip files in the current directory for a given class file

    

/* 
 * The QueryForm License, Version 1.1
 *
 * Copyright (c) 1998 - 2003 David F. Glasser.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. 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.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by 
 *        David F. Glasser."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "QueryForm" and "David F. Glasser" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact dglasser@pobox.com.
 *
 * 5. Products derived from this software may not be called "QueryForm",
 *    nor may "QueryForm" appear in their name, without prior written
 *    permission of David F. Glasser.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 DAVID F. GLASSER, THE APACHE SOFTWARE 
 * FOUNDATION OR ITS CONTRIBUTORS, OR ANY AUTHORS OR DISTRIBUTORS
 * OF THIS SOFTWARE 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.
 * 
 *
 * This product includes software developed by the
 * Apache Software Foundation (http://www.apache.org/).
 *
 * 
 *
 * $Source: /cvsroot/qform/qform/src/org/glasser/util/JarSearcher.java,v $
 * $Revision: 1.1 $
 * $Author: dglasser $
 * $Date: 2003/01/25 23:17:02 $
 * 
 * --------------------------------------------------------------------
 */

import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.io.*;

/**
 * This program will search all jar and zip files in the current directory for
 * a given class file.
 * 
 * @author Dave Glasser
 */
public class JarSearcher {

    /**
     * Returns true if the jar or zip file at jarFilePath contains (with the internal
     * path) the file named by classFilePath. 
     */
    public static boolean searchJarFile(String jarFilePath, String classFilePath) {
        return searchJarFile( new File(jarFilePath), classFilePath);
    }

    public static boolean searchJarFile(File file, String classFilePath) {

        try {
            if(!file.exists()) return false;
    
            ZipFile jarFile = new ZipFile(file);
            if(jarFile.getEntry(classFilePath) != null) {
                jarFile.close();
                return true;
            }
            else {
                jarFile.close();
                return false;
            }
        }
        catch(IOException ex) {
            System.out.println(ex.toString());
            return false;
        }
    }




  static class ArchiveFilter implements FileFilter {

    public boolean accept(File pathName) {
      String upcase = pathName.getName().toUpperCase();
      if(upcase.endsWith(".ZIP") || upcase.endsWith(".JAR")) return true;
      return false;
    }
  }


    public static void main (String[] args)  {
        if(args.length == 0) {
            System.out.println("usage: java ClassFinder <class name>\n\n"
                               + "example: java ClassFinder java.lang.String\n");
            System.exit(0);
        }

        File cwd = new File(".");
        File[] archives = cwd.listFiles(new ArchiveFilter());
    
        String classFileName = args[0].replace('.', '/');
        if(classFileName.endsWith(".class") == false) {
            classFileName += ".class";
        }
    
        System.out.println("Searching for " + classFileName + " ...");
        for(int j=0; j<archives.length; j++) {
    //      System.out.println("Searching " + archives[j].getName());
          if(searchJarFile(archives[j], classFileName)) {
            System.out.println("FOUND IN " + archives[j].getName());
          }
        }
    }
}

   
    
    
    
  








Related examples in the same category

1.Load an Image from a JAR file
2.List files in a jar file
3.Reading a text file from a jar file without unzipping
4.Load an Icon from a jar
5.Creating a JAR File
6.Sign jar with the certificate named alias in the keystore
7.JAR Archives: Jar Lister
8.JAR Archives: Packer200
9.JAR Archives: Unpacker200
10.Listing the Entries of a JAR File Manifest
11.Listing the Main Attributes in a JAR File Manifest
12.Retrieves the manifest from a JAR file and writes the manifest contents to a file.
13.Create Jar file
14.Get the jar file from a URL
15.Get the jar file
16.Get the jar entry
17.Getting a Jar File Using a URL
18.When no entry is specified on the URL, the entry name is null
19.Get the entry name; it should be the same as specified on URL
20.Manifest Writer
21.Load resource from Jar file
22.Jar Entry OutputStream
23.Helper Class to manipulate Java Archive File
24.Some utility classes for manipulating JAR files
25.Retreive Text File From Jar
26.Retreive Binary File From Jar
27.Zip jar Imploder
28.InstallJars - a utility to download and install files, Jars and Zips.
29.Get resource from Jar file
30.class for exploding jar/zip files onto the file system
31.Create a URL that refers to a jar file in the file system
32.Create a URL that refers to an entry in the jar file
33.Unjar a file
34.Jar file helper to deployment
35.Make Temp Jar
36.Determine whether a file is a JAR File.
37.Search class in class path and Jar files
38.Add a jar entry to the deployment archive
39.Add jar contents to the deployment archive under the given prefix
40.Jarring and unjarring files and directories.
41.Create a Jar archive containing the src file/directory
42.A class to find resources in the classpath by their mime-type specified in the MANIFEST.
43.Jar builder
44.Writes all files of the given directory to the specified jar-file