gpt4 book ai didi

java - 如何使用命令行工具按类名查找导入完整路径?

转载 作者:太空宇宙 更新时间:2023-11-04 10:24:19 27 4
gpt4 key购买 nike

使用javap,我可以做到这一点:

xb@dnxb:/tmp$ javap 'java.util.Base64'
Compiled from "Base64.java"
public class java.util.Base64 {
public static java.util.Base64$Encoder getEncoder();
public static java.util.Base64$Encoder getUrlEncoder();
public static java.util.Base64$Encoder getMimeEncoder();
public static java.util.Base64$Encoder getMimeEncoder(int, byte[]);
public static java.util.Base64$Decoder getDecoder();
public static java.util.Base64$Decoder getUrlDecoder();
public static java.util.Base64$Decoder getMimeDecoder();
}
xb@dnxb:/tmp$

但是如何在不知道类名位于 java.util.* 下的情况下进行搜索? (使用命令行实用程序,无需 java 编码)

xb@dnxb:/tmp$ javap 'Base64'
Error: class not found: Base64
xb@dnxb:/tmp$ javap '*.Base64'
Error: class not found: *.Base64
xb@dnxb:/tmp$ javap '*Base64'
Error: class not found: *Base64
xb@dnxb:/tmp$

最佳答案

我发现我可以用这种方式创建我的 map :

xb@dnxb:/tmp$ java -verbose |& grep rt.jar | head -1 #to find out java runtime path
[Opened /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar]
xb@dnxb:/tmp$ unzip /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar -d /tmp/rt #unzip the rt.jar
creating: /tmp/rt/META-INF/
extracting: /tmp/rt/META-INF/MANIFEST.MF
extracting: /tmp/rt/com/oracle/net/Sdp$1.class
extracting: /tmp/rt/com/oracle/net/Sdp$SdpSocket.class
...
extracting: /tmp/rt/java/io/Serializable.class
extracting: /tmp/rt/java/lang/String.class
extracting: /tmp/rt/java/lang/Object.class
xb@dnxb:/tmp$
xb@dnxb:/tmp$ find /tmp/rt > /tmp/rt.map #create list path as map
xb@dnxb:/tmp$

,然后 grep (可能更好地将其作为 shell 函数):

xb@dnxb:/tmp$ grep -rni Base64\.class /tmp/rt.map 
1839:/tmp/rt/java/util/Base64.class
2276:/tmp/rt/java/util/prefs/Base64.class
10340:/tmp/rt/com/sun/xml/internal/messaging/saaj/util/Base64.class
13363:/tmp/rt/com/sun/org/apache/xml/internal/security/utils/Base64.class
15044:/tmp/rt/com/sun/org/apache/xerces/internal/impl/dv/xs/Base64BinaryDV$XBase64.class
15096:/tmp/rt/com/sun/org/apache/xerces/internal/impl/dv/util/Base64.class
xb@dnxb:/tmp$

请注意,/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/classlist 仅包含约 2559 行,与 /tmp/rt.map 中的约 20706 行不同,因此我无法直接 grep classlist 文件。

关于java - 如何使用命令行工具按类名查找导入完整路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50724363/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com