Reputation: 131
I am trying to print all the public methods of classes of a package in a jar file, as of now I only have been able to print class names using:
jar -tf my.jar
But now I want to print all the methods of all classes of a package in a jar file how can i do this using command prompt.
I have seen javap command but its not working. I couldn't find an example of what I want.
Upvotes: 4
Views: 14365
Reputation: 101
#!/bin/sh
jar tf your.jar | grep '.class$' | tr / . | sed 's/\.class$//' | xargs javap -public -cp your.jar
What this does is: It lists the content of your.jar, filters out everything that doesn't end in .class, replaces all slashes with periods, and strips off the .class suffix. This gives us all the classes in your.jar in a form that can be passed to javap, which we do.
With the classpath set to your.jar, javap disassembles each class that we pass it (which is all of them), and prints eventhing that is publicly accessible in them, or in other words: the public API of your.jar.
Upvotes: 3
Reputation: 1407
jar tf
will list the contents for you.
javap
will allow you to see more details of the classes (see the tools guide here).
For instance if you have a class named mypkg.HelloWorld in a jar myjar.jar then run it like
javap -classpath myjar.jar mypkg.HelloWorld
Upvotes: 7
Reputation: 8396
Extract the class from jar file and then run
unzip my.jar
find -name '*.class' | xargs javap -p > myjar.txt
The myjar.txt file will have all members inside the classes inside jar. You can search it for a method right after.
Upvotes: 1