如何在大量jar文件中找到特定的类名?
(查找实际的类名,而不是引用它的类。)
如何在大量jar文件中找到特定的类名?
(查找实际的类名,而不是引用它的类。)
当前回答
我一直在Windows上使用它,效果非常好。
findstr /s /m /c:"package/classname" *.jar, where
findstr.exe是Windows和参数的标准配置:
/s =递归 /m =如果有匹配,只打印文件名 /c =字面字符串(在这种情况下,你的包名+类名 以“/”分隔)
希望这能帮助到一些人。
其他回答
当我遇到这个问题时,我不知道有什么实用程序可以做到这一点,所以我写了下面的代码:
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();
}
}
}
有点晚了,但无论如何……
我一直在使用JarBrowser查找在哪个jar中存在一个特定的类。它有一个易于使用的图形用户界面,允许您浏览所选路径中所有罐子的内容。
Eclipse可以做到这一点,只需创建一个(临时的)项目,并将您的库放在项目类路径上。然后你可以很容易地找到课程。
我想到的另一个工具是Java反编译器。它可以一次打开很多罐子,还可以帮助查找类。
User1207523的脚本对我来说很好。这里有一个变体,使用find隐式搜索jar文件,而不是简单的展开;
#!/bin/bash
for i in `find . -name '*.jar'`; do jar -tf "$i" | grep $1 | xargs -I{} echo -e "$i : {}" ; done
下面的脚本将帮助您
for file in *.jar
do
# do something on "$file"
echo "$file"
/usr/local/jdk/bin/jar -tvf "$file" | grep '$CLASSNAME'
done