How to get method signatures from a jar file?
Solution 1:
jar tf
will list the contents for you. javap
will allow you to see more details of the classes (see the tools guide).
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
However, are you sure you want to be using these unpublished APIs? It's usually a really bad idea.
Solution 2:
As Scott said, you can use Eclipse to get a lot of what you're looking for.
I would recommend getting the JadClipse plugin which will decompile the .class files on the fly and show you actual Java code as you browse the classes in the IDE.
Solution 3:
If you're using eclipse, you can just add it to a project's classpath and explore it using the treeview and/or content assist.
I'd assume other IDEs can do similar.
From a command-line point of view, you can unjar it (jar xf foo.jar) and use javap against all files.
Solution 4:
You can use the library WALA to read out all methods signatures. You'll however need to load Stub-Code for Java first. The following program should read out all the signatures:
import com.ibm.wala.ipa.cha.ClassHierarchy;
import com.ibm.wala.ipa.cha.IClassHierarchy;
import com.ibm.wala.classLoader.IClass;
import com.ibm.wala.classLoader.IMethod;
import com.ibm.wala.ipa.callgraph.AnalysisOptions;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
import com.ibm.wala.types.ClassLoaderReference;
import java.util.jar.JarFile;
import java.io.IOException;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
public class methods {
public static void main(String[] args) throws IOException, ClassHierarchyException {
AnalysisScope scope = AnalysisScope.createJavaAnalysisScope();
scope.addToScope(ClassLoaderReference.Primordial, new JarFile("jSDG-stubs-jre1.5.jar"));
scope.addToScope(ClassLoaderReference.Application, new JarFile("myProgram.jar"));
IClassHierarchy cha = ClassHierarchy.make(scope);
for (IClass cl : cha) {
if (cl.getClassLoader().getReference().equals(ClassLoaderReference.Application)) {
for (IMethod m : cl.getAllMethods()) {
String ac = "";
if (m.isAbstract()) ac = ac + "abstract ";
if (m.isClinit()) ac = ac + "clinit ";
if (m.isFinal()) ac = ac + "final ";
if (m.isInit()) ac = ac + "init ";
if (m.isNative()) ac = ac + "native ";
if (m.isPrivate()) ac = ac + "private ";
if (m.isProtected()) ac = ac + "protected ";
if (m.isPublic()) ac = ac + "public ";
if (m.isSynchronized()) ac = ac + "synchronized ";
System.out.println(ac + m.getSignature());
}
}
}
}
}
If you use the adapted WALA-version from here it does Dalvik (e.g. Android Apps) as well.
Solution 5:
Eclipse would work great
A Java Decompiler would translate the classes back into some semblence of source code that you could study to learn about the classes, the methods, their signatures, and maybe even some insight into valid values for some arguments (e.g. don't pass a null for this argument or you'll trigger a NullPointerException immediately). But roll up your sleeves to unjar the jar and run the decompiler against all the class files. This is essentially what Eclipse is doing for help text with undocumented classes.
Finally, of course, a "real programmer" would read the byte-code directly without need for a decompiler.