License Public Domain
Lines 70
Average
4.0
Rated
1
Times
5
4
3
2
1
0
Keywords
class (8) jar (1) java (8) search (1)
Permissions
Owner: jlugocp
Viewable by Everyone
Editable by All Siafoo Users
Hide
Siafoo is here to make coding less frustrating and to save you time. Join Siafoo Now or Learn More

A Java program to search for classes in JAR files Atom Feed

In Brief Searches for class path in all JAR files inside the specified directory. Class path can be partial.... more
# 's
 1import java.util.jar.*;
2import java.util.*;
3import java.io.*;
4
5
6public class JarSearch {
7
8 public static final String NL = System.getProperty("line.separator");
9
10 public static void main(String args[]) {
11 if (args.length != 2){
12 System.out.println("You must provide 1. search directory and 2. search string");
13 System.exit(0);
14 }
15 JarSearch search = new JarSearch();
16 try{
17 String dir = args[0];
18 String searchStr = args[1];
19 search.searchDir(dir, searchStr);//"C:\\Program Files\\IBM\\Application Developer\\plugins\\com.ibm.etools.ctc.binding.java\\runtime\\ctcjava.jar");
20 }catch(IOException ioe){
21 System.out.println(ioe.toString());
22 }
23 }
24
25 public static void searchDir(String dirName, String searchStr)throws IOException{
26 File dir = new File(dirName);
27 if (!dir.isDirectory()){
28 throw new IOException(dir+" is not a directory !!!");
29 }
30 File files[] = dir.listFiles();
31 for (int i=0; i<files.length; i++){
32 if (files[i].isDirectory()){
33 searchDir(files[i].getAbsolutePath(), searchStr);
34 }else{
35 if (files[i].getName().endsWith(".jar")){
36 searchFile(files[i].getAbsolutePath(), searchStr);
37 }
38 }
39 }
40 }
41
42 public static void searchFile(String fileName, String searchStr)throws IOException{
43 boolean outputMessage = false;
44 StringBuffer buf = new StringBuffer();
45 int index1 = fileName.lastIndexOf("\\");
46 int index2 = fileName.lastIndexOf("/");
47 int index = -1;
48 if (index1 > index2){
49 index = index1;
50 }else{
51 index = index2;
52 }
53 buf.append(NL);
54
55 buf.append("DIR: ");
56 buf.append(fileName.substring(0, index));
57 buf.append(NL);
58
59 buf.append("JAR: ");
60 buf.append(fileName.substring(index+1));
61 buf.append(NL);
62
63 searchStr = searchStr.toLowerCase();
64
65 JarFile jar = new JarFile(fileName);
66 Enumeration e = jar.entries();
67 while (e.hasMoreElements()){
68 JarEntry entry = (JarEntry)e.nextElement();
69 String strEntry = entry.getName().toLowerCase();
70 if (strEntry.indexOf(searchStr) > -1){
71 outputMessage = true;
72 buf.append(entry.getName());
73 buf.append(NL);
74 }
75 }
76 if (outputMessage){
77 System.out.println(buf);
78 }
79 }
80}

Searches for class path in all JAR files inside the specified directory. Class path can be partial.

Example usages ... java JarSearch /parent/dir/of/jars StringUtils java JarSearch /parent/dir/of/jars lang/StringUt