如何在大量jar文件中找到特定的类名?
(查找实际的类名,而不是引用它的类。)
如何在大量jar文件中找到特定的类名?
(查找实际的类名,而不是引用它的类。)
当前回答
要在一个jar文件夹(及其子文件夹)中找到一个类: https://jarscan.com/
Usage: java -jar jarscan.jar [-help | /?]
[-dir directory name]
[-zip]
[-showProgress]
<-files | -class | -package>
<search string 1> [search string 2]
[search string n]
Help:
-help or /? Displays this message.
-dir The directory to start searching
from default is "."
-zip Also search Zip files
-showProgress Show a running count of files read in
-files or -class Search for a file or Java class
contained in some library.
i.e. HttpServlet
-package Search for a Java package
contained in some library.
i.e. javax.servlet.http
search string The file or package to
search for.
i.e. see examples above
例子:
java -jar jarscan.jar -dir C:\Folder\To\Search -showProgress -class GenericServlet
其他回答
查看JBoss Tattletale;虽然我个人从未使用过,但这似乎是你需要的工具。
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文件的搜索引擎。
你可以使用locate和grep:
locate jar | xargs grep 'my.class'
确保在使用locate之前运行updatedb。
文件名: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”调用它。
单击以查看示例输出
使用unzip (zipinfo)的bash脚本解决方案。在Ubuntu 12上测试。
#!/bin/bash
# ./jarwalker.sh "/a/Starting/Path" "aClassName"
IFS=$'\n'
jars=( $( find -P "$1" -type f -name "*.jar" ) )
for jar in ${jars[*]}
do
classes=( $( zipinfo -1 ${jar} | awk -F '/' '{print $NF}' | grep .class | awk -F '.' '{print $1}' ) )
if [ ${#classes[*]} -ge 0 ]; then
for class in ${classes[*]}
do
if [ ${class} == "$2" ]; then
echo "Found in ${jar}"
fi
done
fi
done