如何在大量jar文件中找到特定的类名?
(查找实际的类名,而不是引用它的类。)
如何在大量jar文件中找到特定的类名?
(查找实际的类名,而不是引用它的类。)
当前回答
Grepj是一个命令行实用程序,用于在jar文件中搜索类。我是这个实用程序的作者。
你可以运行grepj package.Class my1.jar my2这样的实用程序。战争my3.ear
可以提供多个jar, ear, war文件。对于高级用法,使用find提供要搜索的jar列表。
其他回答
Eclipse可以做到这一点,只需创建一个(临时的)项目,并将您的库放在项目类路径上。然后你可以很容易地找到课程。
我想到的另一个工具是Java反编译器。它可以一次打开很多罐子,还可以帮助查找类。
Grepj是一个命令行实用程序,用于在jar文件中搜索类。我是这个实用程序的作者。
你可以运行grepj package.Class my1.jar my2这样的实用程序。战争my3.ear
可以提供多个jar, ear, war文件。对于高级用法,使用find提供要搜索的jar列表。
要搜索给定目录下的所有jar文件以查找特定的类,你可以这样做:
ls *.jar | xargs grep -F MyClass
或者更简单一点,
grep -F MyClass *.jar
输出如下所示:
Binary file foo.jar matches
它非常快,因为-F选项意味着搜索Fixed字符串,所以它不会为每次grep调用加载regex引擎。如果需要,您总是可以省略-F选项并使用正则表达式。
当我遇到这个问题时,我不知道有什么实用程序可以做到这一点,所以我写了下面的代码:
public class Main {
/**
*
*/
private static String CLASS_FILE_TO_FIND =
"class.to.find.Here";
private static List<String> foundIn = new LinkedList<String>();
/**
* @param args the first argument is the path of the file to search in. The second may be the
* class file to find.
*/
public static void main(String[] args) {
if (!CLASS_FILE_TO_FIND.endsWith(".class")) {
CLASS_FILE_TO_FIND = CLASS_FILE_TO_FIND.replace('.', '/') + ".class";
}
File start = new File(args[0]);
if (args.length > 1) {
CLASS_FILE_TO_FIND = args[1];
}
search(start);
System.out.println("------RESULTS------");
for (String s : foundIn) {
System.out.println(s);
}
}
private static void search(File start) {
try {
final FileFilter filter = new FileFilter() {
public boolean accept(File pathname) {
return pathname.getName().endsWith(".jar") || pathname.isDirectory();
}
};
for (File f : start.listFiles(filter)) {
if (f.isDirectory()) {
search(f);
} else {
searchJar(f);
}
}
} catch (Exception e) {
System.err.println("Error at: " + start.getPath() + " " + e.getMessage());
}
}
private static void searchJar(File f) {
try {
System.out.println("Searching: " + f.getPath());
JarFile jar = new JarFile(f);
ZipEntry e = jar.getEntry(CLASS_FILE_TO_FIND);
if (e == null) {
e = jar.getJarEntry(CLASS_FILE_TO_FIND);
if (e != null) {
foundIn.add(f.getPath());
}
} else {
foundIn.add(f.getPath());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
查找匹配给定字符串的jar:
找到。name \*.jar -exec grep -l YOUR_CLASSNAME {} \;