如何在大量jar文件中找到特定的类名?
(查找实际的类名,而不是引用它的类。)
如何在大量jar文件中找到特定的类名?
(查找实际的类名,而不是引用它的类。)
当前回答
#!/bin/bash
pattern=$1
shift
for jar in $(find $* -type f -name "*.jar")
do
match=`jar -tvf $jar | grep $pattern`
if [ ! -z "$match" ]
then
echo "Found in: $jar"
echo "$match"
fi
done
其他回答
文件名: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”调用它。
单击以查看示例输出
Unix
在Linux、其他Unix变体、Windows上的Git Bash或Cygwin上,使用jar(或unzip -v)、grep和find命令。
下面列出了与给定名称匹配的所有类文件:
for i in *.jar; do jar -tvf "$i" | grep -Hsi ClassName && echo "$i"; done
如果知道要搜索的整个Java存档列表,可以使用(符号)链接将它们全部放在同一个目录中。
或者使用find(区分大小写)查找包含给定类名的JAR文件:
find path/to/libs -name '*.jar' -exec grep -Hls ClassName {} \;
例如,要查找包含IdentityHashingStrategy的存档的名称:
$ find . -name '*.jar' -exec grep -Hsli IdentityHashingStrategy {} \;
./trove-3.0.3.jar
如果JAR可能在系统中的任何位置,并且locate命令可用:
for i in $(locate "*.jar");
do echo "$i"; jar -tvf "$i" | grep -Hsi ClassName;
done
语法变化:
find path/to/libs -name '*.jar' -print | \
while read i; do jar -tvf "$i" | grep -Hsi ClassName && echo "$i"; done
窗户
打开命令提示符,切换到包含JAR文件的目录(或祖先目录),然后:
for /R %G in (*.jar) do @jar -tvf "%G" | find "ClassName" > NUL && echo %G
下面是它的工作原理:
for /R %G in (*.jar) do - loop over all JAR files, recursively traversing directories; store the file name in %G. @jar -tvf "%G" | - run the Java Archive command to list all file names within the given archive, and write the results to standard output; the @ symbol suppresses printing the command's invocation. find "ClassName" > NUL - search standard input, piped from the output of the jar command, for the given class name; this will set ERRORLEVEL to 1 iff there's a match (otherwise 0). && echo %G - iff ERRORLEVEL is non-zero, write the Java archive file name to standard output (the console).
Web
使用扫描JAR文件的搜索引擎。
我找到了这个新方法
bash $ ls -1 | xargs -i -t jar -tvf '{}'| grep Abstract
jar -tvf activation-1.1.jar
jar -tvf antisamy-1.4.3.jar
2263 Thu Jan 13 21:38:10 IST 2011 org/owasp/validator/html/scan/AbstractAntiSamyScanner.class
...
如果你想,你可以给ls -1 *.jar,或者用查找命令HTH Someone输入xargs。
Eclipse可以做到这一点,只需创建一个(临时的)项目,并将您的库放在项目类路径上。然后你可以很容易地找到课程。
我想到的另一个工具是Java反编译器。它可以一次打开很多罐子,还可以帮助查找类。
我知道这是个老问题,但是…… 我也有同样的问题,所以如果有人正在寻找一个非常简单的解决方案的windows -有一个软件名为Locate32 只要把jar作为文件扩展名 并包含类名 它会在几秒钟内找到文件。