I often use open source things. I have a very annoying place that I don't know which jar is introduced from. It is bored tonight. I studied the java.util.zip package in JDK. I wrote a Small things can search all JAR files in a directory to see if there is a specified class name or package name.
code show as below:
Import java.io. *;
Import java.util.zip. *;
Import java.util. *;
Public Class Jarsearch
{
Public static final char file_seperator = '/';
Public static final char package_seperator = '.';
Private static void printhelp () {
System.out.println ("USAGE:");
System.out.println ("Java Jarsearch -d [Directory] -c [classname]");
System.out.println ("Java jarsearch -f [filename] -c [classname]");
System.out.println ("Java Jarsearch -d [Directory] -p [packagename]");
System.out.println ("Java jarsearch -f [filename] -p [packagename]");
}
Private static string getshfortfilename (String longname) {
StringTokenizer ST = New StringTokenizer (longname, "/");
String s = null;
While (st.hasmoretokens ()) {
s = st.nextToken ();
}
Return S;
}
Private static void Findclass (String Jarfile, String ClassName) throws exception {
FILE F = New File (Jarfile);
IF (f.IsDirectory ()) Return;
String fn = f.getname ();
IF (fn.length () <= 4) return;
IF (! (fn.substring (fn.length () - 4, fn.length ())). Equalsignorecase (". jar"))))))
Return;
ZipinputStream Zipin = New FileInputStream (New FileInputStream (Jarfile);
ZIPENTRY Entry = NULL;
String name = NULL;
While ("(entry = zipin.getnextentry ())! = null) {
IF (entry.IsDirectory ()) continue;
Name = getshortfilename (entry.getname ());
IF (name.startswith) {
System.out.println ("************************************************************************************************");
System.out.println ("Found In Jarfile:" JARFILE);
System.out.Println ("**************************************************************************************
}
}
System.out.println ("No found in jarfile:" JARFILE);
Zipin.close ();
}
Private static void FindPackage (String Jarfile, String PackageName) throws Exception {
FILE F = New File (Jarfile);
IF (f.IsDirectory ()) Return;
String fn = f.getname ();
IF (fn.length () <= 4) return;
IF (! (fn.substring (fn.length () - 4, fn.length ())). Equalsignorecase (". jar"))))))
Return;
ZipinputStream Zipin = New FileInputStream (New FileInputStream (Jarfile);
ZIPENTRY Entry = NULL;
String name = NULL;
While ("(entry = zipin.getnextentry ())! = null) {
IF (entry.IsDirectory ()) continue;
Name = (entry.getname ()). Replace (file_seperator, package_seperator);
IF (Name.startSwith) {
System.out.println ("************************************************************************************************");
System.out.println ("Found In Jarfile:" JARFILE);
System.out.println ("************************************************************************************************");
Return;
}
}
System.out.println ("No found in jarfile:" JARFILE);
Zipin.close ();
}
Private static void FindPackageIndir (String Jardir, String PackageName) throws exception {
File mydir = new file (jardir);
File F;
String [] filenames = mydir.list ();
For (int i = 0; i f = new file (Jardir File.seParetor FileNames [i]); IF (f.IsDirectory ()) { FindPackageindir (Jardir File.seParetor FileNames [I], PackageName); } Else { FINDPACKAGE (Jardir File.seParetor FileNames [i], packagename); } } } Private static void findir (String Jardir, String ClassName) throws exception { File mydir = new file (jardir); File F; String [] filenames = mydir.list (); for (int i = 0; i f = new file (Jardir File.seParetor FileNames [i]); IF (f.IsDirectory ()) { FindIndir (Jardir File.seParetor FileNames [i], classname); } Else { FindClass (Jardir File.seParetor FileNames [I], ClassName); } } } Public static void main (string [] args) { IF (args.length! = 4) { Printhelp (); System.exit (0); } Try { IF (args [0]. Equalsignorecase ("- d") && args [2] .equalsignorecase ("- c")) { FindIndir (Args [1], Args [3]); } IF (args [0]. Equalsignorecase ("- f") && args [2] .equalsignorecase ("- c")) { Findclass (Args [1], Args [3]); } IF (args [0]. Equalsignorecase ("- d") && args [2] .equalsignorecase ("- p")) { FINDPACKAGEINDIR (Args [1], Args [3]); } IF (args [0]. Equalsignorecase ("- f") && args [2] .equalsignorecase ("- p")) { FINDPACKAGE (Args [1], Args [3]); } } Catch (Exception E) { E.PrintStackTrace (); } } }