如何在大量jar文件中找到特定的类名?
(查找实际的类名,而不是引用它的类。)
如何在大量jar文件中找到特定的类名?
(查找实际的类名,而不是引用它的类。)
当前回答
文件名:searchForFiles.py
import os, zipfile, glob, sys
def main():
searchFile = sys.argv[1] #class file to search for, sent from batch file below (optional, see second block of code below)
listOfFilesInJar = []
for file in glob.glob("*.jar"):
archive = zipfile.ZipFile(file, 'r')
for x in archive.namelist():
if str(searchFile) in str(x):
listOfFilesInJar.append(file)
for something in listOfFilesInJar:
print("location of "+str(searchFile)+": ",something)
if __name__ == "__main__":
sys.exit(main())
你可以通过创建一个带有以下文本的.bat文件(用你正在搜索的文件替换“AddWorkflows.class”)来轻松运行:
(文件:CallSearchForFiles.bat)
@echo off
python -B -c "import searchForFiles;x=searchForFiles.main();" AddWorkflows.class
pause
您可以双击CallSearchForFiles.bat来运行它,或者从命令行“CallSearchForFiles.bat SearchFile.class”调用它。
单击以查看示例输出
其他回答
除此之外,还有一件事需要补充:如果您没有可用的jar可执行文件(它随JDK而不是JRE一起提供),您可以使用unzip(或WinZip或其他任何工具)来完成同样的事情。
有点晚了,但无论如何……
我一直在使用JarBrowser查找在哪个jar中存在一个特定的类。它有一个易于使用的图形用户界面,允许您浏览所选路径中所有罐子的内容。
你可以在一个充满jar的目录中找到一个类,它带有一些shell:
寻找类“FooBar”:
LIB_DIR=/some/dir/full/of/jarfiles
for jarfile in $(find $LIBDIR -name "*.jar"); do
echo "--------$jarfile---------------"
jar -tvf $jarfile | grep FooBar
done
在eclipse中,你可以使用旧的但仍然可用的插件jarsearch
使用这个. .你可以在类路径..中找到任何文件。保证. .
import java.net.URL;
import java.net.URLClassLoader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class FileFinder {
public static void main(String[] args) throws Exception {
String file = <your file name>;
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)cl).getURLs();
for(URL url: urls){
listFiles(file, url);
}
}
private static void listFiles(String file, URL url) throws Exception{
ZipInputStream zip = new ZipInputStream(url.openStream());
while(true) {
ZipEntry e = zip.getNextEntry();
if (e == null)
break;
String name = e.getName();
if (name.endsWith(file)) {
System.out.println(url.toString() + " -> " + name);
}
}
}
}