View Javadoc

1   /*
2    * $Id JarFinder.java $
3    *
4    */
5   
6   package org.astrogrid.devtools;
7   
8   
9   /***
10   * Locates the jar file in a subdirectory of this one that has the given
11   * class in it.  Used for all those nasty NoClassDefFoundError which means
12   * you haven't included some jar file in your classpath but you have no idea
13   * which one it is.
14   * <p>
15   * This does of course mean that you have to have that jar file present (so
16   * it can be found) but you can run this on a working system to find the name
17   * of it
18   * <p>
19   *
20   * @author M Hill
21   */
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.util.Enumeration;
26  import java.util.jar.JarFile;
27  import java.util.zip.ZipEntry;
28  import org.astrogrid.util.ClassPathUtils;
29  
30  public class JarFinder
31  {
32     /***
33      * Looks for the class in the classpath - has ready made found jars/directories
34      */
35     public static String findInClasspath(String className) throws IOException
36     {
37  //      try
38  //      {
39  //         Class givenClass = JarFinder.class.forName(className);
40           
41           String[] paths = ClassPathUtils.getClassPathList();
42           
43           for (int i=0; i<paths.length;i++)
44           {
45              String path = paths[i];
46              
47              if (path.endsWith(".jar"))
48              {
49                 if (isClassInJar(className, path))
50                 {
51                    return path;
52                 }
53              }
54           }
55           
56           return null;
57  //      }
58  //      catch (ClassNotFoundException e)
59  //      {
60  //         return "Class "+className+" not loadable";
61  //      }
62     }
63  
64     /***
65      * Looks to see if the given class is in the given jar file.  */
66     public static boolean isClassInJar(String className, String jarPath) throws IOException
67     {
68        JarFile jarFile = new JarFile(new File(jarPath));
69        
70        Enumeration entries = jarFile.entries();
71        
72        while (entries.hasMoreElements())
73        {
74           ZipEntry entry = (ZipEntry) entries.nextElement();
75            
76           String entryName = entry.getName();
77           
78           if (entryName.indexOf(className) >-1)
79           {
80              System.out.println("Found "+entryName+" in "+jarPath);
81              return true;
82           }
83           
84        }
85        
86        
87        return false;
88     }
89     
90     
91     
92     
93     /***
94      * For running from the command line
95      */
96     public static void main(String[] args) throws IOException
97     {
98        String classname = args[0];
99  //      String classname = "DocumentRange";
100       String jar = findInClasspath(classname);
101       System.out.println(jar);
102       //findInDirectory(classname);
103    }
104 }
105 
106 /*
107 $Log: JarFinder.java,v $
108 Revision 1.4  2004/02/17 14:31:49  mch
109 Minor changes to please checkstyle
110 
111 Revision 1.3  2003/12/02 19:52:53  mch
112 For running from the command line
113 
114 Revision 1.2  2003/09/24 18:33:13  mch
115 Wrong previous checkin message: this is for finding which jar a class is in
116 
117 Revision 1.1  2003/09/24 18:31:55  mch
118 Tool for checking classpath
119 
120 */
121