如何在大量jar文件中找到特定的类名?
(查找实际的类名,而不是引用它的类。)
如何在大量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
其他回答
下面的脚本将帮助您
for file in *.jar
do
# do something on "$file"
echo "$file"
/usr/local/jdk/bin/jar -tvf "$file" | grep '$CLASSNAME'
done
不久前,我专门为此写了一个程序:https://github.com/javalite/jar-explorer
使用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
要搜索给定目录下的所有jar文件以查找特定的类,你可以这样做:
ls *.jar | xargs grep -F MyClass
或者更简单一点,
grep -F MyClass *.jar
输出如下所示:
Binary file foo.jar matches
它非常快,因为-F选项意味着搜索Fixed字符串,所以它不会为每次grep调用加载regex引擎。如果需要,您总是可以省略-F选项并使用正则表达式。
我一直在Windows上使用它,效果非常好。
findstr /s /m /c:"package/classname" *.jar, where
findstr.exe是Windows和参数的标准配置:
/s =递归 /m =如果有匹配,只打印文件名 /c =字面字符串(在这种情况下,你的包名+类名 以“/”分隔)
希望这能帮助到一些人。