java搜索.class_java搜索类所在的包
import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class FindJarForClass {
public String className;
public
ArrayList jarFiles = new
ArrayList();
public FindJarForClass() {
}
public FindJarForClass(String
className) {
this.className =
className;
}
public void setClassName(String
className) {
this.className =
className;
}
public
List findClass(String dir,
boolean recurse) {
searchDir(dir, recurse);
return this.jarFiles;
}
protected void searchDir(String dir,
boolean recurse) {
try {
File d = new
File(dir);
if
(!d.isDirectory()) {
return;
}
File[] files
= d.listFiles();
for (int i =
0; i < files.length; i++) {
if
(recurse && files[i].isDirectory())
{
searchDir(files[i].getAbsolutePath(),
true);
}
else {
String
filename = files[i].getAbsolutePath();
if
(filename.endsWith(".jar") || filename.endsWith(".zip")) {
ZipFile
zip = new ZipFile(filename);
Enumeration
extends ZipEntry> entries = zip.entries();
while
(entries.hasMoreElements()) {
ZipEntry
entry = (ZipEntry) entries.nextElement();
String
thisClassName = getClassName(entry);
if
(thisClassName.equals(this.className)
||
thisClassName.equals(this.className
+
".class")) {
this.jarFiles.add(filename);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public
List getFilenames() {
return this.jarFiles;
}
protected String getClassName(ZipEntry
entry) {
StringBuffer className = new
StringBuffer(entry.getName().replace('/',
'.'));
return
className.toString();
}
public static void main(String args[])
{
System.out.println("请输入要查找的类全名,如
java.util.List");
Scanner scanner = new
Scanner(System.in);
String className =
scanner.nextLine();
System.out.println("您要查找的类名为: "
+ className);
System.out.println("请输入要查找的路径,如
E:\\JVM\\jdk1.6.0_10\\lib");
String serarchPath =
scanner.nextLine();
System.out.println("您要查找的路径为: "
+ serarchPath);
FindJarForClass findInJar = new
FindJarForClass(className);
List
jarFiles = findInJar.findClass(serarchPath, true);
if (jarFiles.size() == 0)
{
System.out.println("Not
Found");
} else {
for (int i =
0; i < jarFiles.size(); i++) {
System.out.println(jarFiles.get(i));
}
}
}
}