22

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.

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?

Prabhu R
  • 13,836
  • 21
  • 78
  • 112

5 Answers5

33
#!/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
David Grant
  • 13,929
  • 3
  • 57
  • 63
  • 6
    javap will happily disasseble class names with `/` instead of `.`, you only need to remove `.class` suffix. I am using `zipinfo -1 ${jar} \*.class| sed 's/\.class//' | xargs javap -classpath "$jar" -c -l -private > ${jar}.javap` – Miserable Variable Jul 18 '14 at 00:18
16

Even easier would be

JAR=<path to jarfile> \
javap -classpath $JAR $(jar -tf $JAR | grep "class$" | sed s/\.class$//)
Bex
  • 2,905
  • 2
  • 33
  • 36
2

First unzip the jar file, this will yield a series of directories for each package, then apply the javap command per directory.

So for example with tomcat you can unzip the catalina-balancer.jar file in webapps\balancer and then use

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.

(It would be fairly easy to write in perl/bash/java).

Rudi Bierach
  • 341
  • 1
  • 3
0

Note: You may/can also need to specify the specific jar, and the package of the class:

In general:

javap -classpath "jarpath.jarname" "package.classname"

In the above example:

javap -classpath RuleContaining.jar org.apache.webapp.balancer.Rule

Roland Roos
  • 1,003
  • 10
  • 4
0

you can also specify the URL for a class in the jar to javap like this:

javap -v jar:file:///path/to/MyJar.jar!/mypkg/MyClass.class

sify
  • 645
  • 9
  • 19