日常调试开发Java应用程序时经常会遇到一个问题:
依赖库缺失或多了多余的依赖,或者版本不对的错误。
如何快速定位这种问题呢? 这个需要一个有力的工具来解决这个问题。
findjar 可以根据类名,在指定目录中搜索是否存在指定的jar文件,
然后返回包含指定类名的jar文件全路径,非常好用。
touch findjar
chmod +x findjar
#!/usr/bin/env bash
if [[ ($# -ne 1) && ($# -ne 2) ]]
then
echo "用法 $0 Java类名称 目录路径 [ 目录路径 如果没有输入就使用当前目录]"
echo "findjar com.google.common.base.Preconditions /tmp/"
else
THING_TO_LOOKFOR="$1"
DIR=${2:-.}
if [ ! -d $DIR ]; then
echo "directory [$DIR] does not exist";
exit 1;
fi
find "$DIR" -iname \*.jar | while read f ; do (jar tf $f | awk '{print "'"$f"'" " " $0}' | grep -i "$THING_TO_LOOKFOR") ; done
fi
```
应用举例:
./findjar com.google.common.base.Preconditions `pwd`
