Linux查找class类所在jar包

找到Integer.class所在的jar包

1.1.使用ls命令版本1

ls *.jar | while read jarfile; do
    echo "$jarfile"
    jar -tf $jarfile | grep "Integer.class"
done

在Linux终端命令行输入如下命令,
然后直接回车即可:

[root@dap211-159 lib]# ls *.jar | while read jarfile; do
>     echo "$jarfile"
>     jar -tf $jarfile | grep "Integer.class"
> done

命令输出结果如下:

charsets.jar
deploy.jar
javaws.jar
jce.jar
jfr.jar
jfxswt.jar
jsse.jar
management-agent.jar
plugin.jar
resources.jar
rt.jar
com/sun/org/apache/bcel/internal/classfile/ConstantInteger.class
com/sun/xml/internal/bind/v2/runtime/reflect/PrimitiveArrayListerInteger.class
com/sun/xml/internal/bind/v2/runtime/reflect/opt/FieldAccessor_Integer.class
com/sun/xml/internal/bind/v2/runtime/reflect/opt/MethodAccessor_Integer.class
com/sun/xml/internal/bind/v2/runtime/reflect/opt/TransducedAccessor_field_Integer.class
com/sun/xml/internal/bind/v2/runtime/reflect/opt/TransducedAccessor_method_Integer.class
sun/java2d/xr/MutableInteger.class
java/math/SignedMutableBigInteger.class
java/math/MutableBigInteger.class
java/math/BigInteger.class
sun/misc/FDBigInteger.class
java/util/concurrent/atomic/AtomicInteger.class
java/lang/Integer.class

可以看到Integer.class上一条输出的jar包是rt.jar,
所以Integer.class是在rt.jar包中的。

1.2.使用ls命令版本2

上一个命令输出的结果太多,不方便查看,
修改为只有找到找到Integer.class类,
才打印其所在的jar包。

ls *.jar | while read jarfile; do
    jar -tf $jarfile | grep  "Integer.class"
    if [ $? -eq 0 ]
    then
        echo "find class in jar : "$jarfile
    fi
done

命令输出结果如下:

com/sun/org/apache/bcel/internal/classfile/ConstantInteger.class
com/sun/xml/internal/bind/v2/runtime/reflect/PrimitiveArrayListerInteger.class
com/sun/xml/internal/bind/v2/runtime/reflect/opt/FieldAccessor_Integer.class
com/sun/xml/internal/bind/v2/runtime/reflect/opt/MethodAccessor_Integer.class
com/sun/xml/internal/bind/v2/runtime/reflect/opt/TransducedAccessor_field_Integer.class
com/sun/xml/internal/bind/v2/runtime/reflect/opt/TransducedAccessor_method_Integer.class
sun/java2d/xr/MutableInteger.class
java/math/SignedMutableBigInteger.class
java/math/MutableBigInteger.class
java/math/BigInteger.class
sun/misc/FDBigInteger.class
java/util/concurrent/atomic/AtomicInteger.class
java/lang/Integer.class
find class in jar : rt.jar

1.3.使用ls命令版本3

上一个命令输出的结果还是有点多,
输出了其他包含Integer.class的类,
可以把grep条件改为/Integer.class,
如果我们知道Integer.class的类全限定名称,
可以把grep条件改为java/lang/Integer.class:

ls *.jar | while read jarfile; do
    jar -tf $jarfile | grep "java/lang/Integer.class"
    if [ $? -eq 0 ]
    then
        echo "find class in jar : "$jarfile
    fi
done

命令输出结果如下:

java/lang/Integer.class
find class in jar : rt.jar

1.4.使用find命令版本

如果要查找的jar包不仅在当前目录下面,
而且在各个子目录下面,
自己并不清楚具体要在那个目录查找,
比如在Maven本地仓库,或者是自己安装的应用服务中查找,
这个时候就可以切换到家目录或者/目录下,
使用find命令在所有jar包中查找指定类:

find . -iname "*.jar" | while read jarfile; do
    jar -tf $jarfile | grep "java/lang/Integer.class"
    if [ $? -eq 0 ]
    then
        echo "find class in jar : "$jarfile
    fi
done

命令输出结果同上。
大部分情况下使用这个命令就可以了。

2.找到Integer.class所在的jar包方法2

下面再提供一个结合awk和sh的方法:

find . -name "*.jar"| awk '{print "jar -tf "$1}'| sh -x | grep -i "/Integer.class"

命令输出结果如下:

+ jar -tf ./jfr.jar
+ jar -tf ./deploy.jar
+ jar -tf ./management-agent.jar
+ jar -tf ./javaws.jar
+ jar -tf ./plugin.jar
+ jar -tf ./charsets.jar
+ jar -tf ./rt.jar
java/lang/Integer.class
+ jar -tf ./resources.jar
+ jar -tf ./security/policy/limited/local_policy.jar
+ jar -tf ./security/policy/limited/US_export_policy.jar
+ jar -tf ./security/policy/unlimited/local_policy.jar
+ jar -tf ./security/policy/unlimited/US_export_policy.jar
+ jar -tf ./jsse.jar
+ jar -tf ./ext/sunec.jar
+ jar -tf ./ext/dnsns.jar
+ jar -tf ./ext/cldrdata.jar
+ jar -tf ./ext/jfxrt.jar
+ jar -tf ./ext/jaccess.jar
+ jar -tf ./ext/nashorn.jar
+ jar -tf ./ext/zipfs.jar
+ jar -tf ./ext/sunpkcs11.jar
+ jar -tf ./ext/localedata.jar
+ jar -tf ./ext/sunjce_provider.jar
+ jar -tf ./jce.jar
+ jar -tf ./jfxswt.jar

可以看到在查看rt.jar时找到了Integer.class类

posted @ 2023-02-13 17:05  車輪の唄  阅读(460)  评论(0编辑  收藏  举报  来源