Question

I have a third-party jar file that comes with the javadocs for only part of the API. Is there a way to reverse engineer the jar file to obtain a complete listing of classes and methods?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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.

  • 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.

a quick help for knowing methods of a normal class (not abstract class),I do the following .

new classname().press ctrl+space for methods listing in eclipse.

Use Eclipse > Package Explorer to see the classes and thier hierarchy.

Content Assist(autocomplete feature (ctrl + space)) is also a good help , but wouldnt recomend using an unpublished API

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top