java 如何使用 javap 工具打印 jar 文件中的类结构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1171549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I print the class structures in a jar file using the javap tool?
提问by Prabhu R
I want to list the methods of the class files in the jar using the javap tool. How do I do it so that it lists the methods and members of all the class files in the jar. Right now I am able to do it for just one class at a time.
我想使用javap工具列出jar中类文件的方法。我该怎么做才能列出 jar 中所有类文件的方法和成员。现在我一次只能为一节课做这件事。
I am expecting something like if I say
我期待着如果我说
javap java.lang.*
it should enlist the methods and members of all the classes in java.lang package. If javap is not capable of that, are there any such tools available?
它应该登记 java.lang 包中所有类的方法和成员。如果 javap 不能做到这一点,是否有任何此类工具可用?
回答by David Grant
#!/bin/bash
# Set the JAR name
jar=<JAR NAME>
# Loop through the classes (everything ending in .class)
for class in $(jar -tf $jar | grep '.class'); do
# Replace /'s with .'s
class=${class//\//.};
# javap
javap -classpath $jar ${class//.class/};
done
回答by Bex
Even easier would be
更容易
JAR=<path to jarfile> \
javap -classpath $JAR $(jar -tf $JAR | grep "class$" | sed s/\.class$//)
回答by Rudi Bierach
First unzip the jar file, this will yield a series of directories for each package, then apply the javap command per directory.
首先解压 jar 文件,这将为每个包生成一系列目录,然后对每个目录应用 javap 命令。
So for example with tomcat you can unzip the catalina-balancer.jar file in webapps\balancer and then use
因此,例如使用 tomcat,您可以解压缩 webapps\balancer 中的 catalina-balancer.jar 文件,然后使用
javap -classpath org\apache\webapp\balancer Rule
which gives
这使
Compiled from "Rule.java"
interface org.apache.webapp.balancer.Rule{
public abstract boolean matches(javax.servlet.http.HttpServletRequest);
public abstract java.lang.String getRedirectUrl();
}
If you need to do this for all the class files in a package you will need to write a script or program to walk the classpath and strip the .class from the filenames and pass it to javap.
如果您需要对包中的所有类文件执行此操作,您将需要编写脚本或程序来遍历类路径并从文件名中去除 .class 并将其传递给 javap。
(It would be fairly easy to write in perl/bash/java).
(用 perl/bash/java 编写相当容易)。

