views:

87

answers:

4

I have a jar file. I want to know which external classes and methods is used by classes inside jar file. Can anyone suggest me any tool? For example

import java.util.Vector;
class MyJarClass{

    public static void main(String args[]){
        Vector v = new Vector();
        AnotherClass another = new AnotherClass();

        v.addElement("one");
        another.doSomething();

    }
}

class AnotherClass{

    void doSomething(){
    }
}

Above two classes are packaged into Myjar.jar

When I supply this jar to a tool, that tool should show java.util.Vector and Vector.adElements() are from external source (not present in MyJar.jar)

Forgot to mention, i dont have access to sourcecode o.

+2  A: 

You might want to look at BCEL, which will allow you to parse the class files and find out what they use. I expect it'll be a certain amount of effort though.

Jon Skeet
A: 

if u edit this files in a intelligent editors like eclipse it shows the use packages

sagar
Really? I've not come across this feature in Eclipse. What menu do I find it in?
Stephen C
@Stephen C - your eclipse is not intelligent enough :-P
Andreas_D
@sagar Which plugin do you use for that? Could you please add a link.
stacker
@Andreas_D - If I were you, I'd be careful about saying rude things about my Eclipse. If you upset it, it might get its dad to beat up your dad. So there! :-P razzz ... :-)
Stephen C
A: 

Check JDepend

The graphical user interface displays the afferent and efferent couplings of each analyzed Java package, presented in the familiar Java Swing tree structure.

stacker
I tried JDepend. I saw only package names. I am not able to see class names and method names.
anupsth
+2  A: 

Easy

import com.example.*;

Possible

List<com.example.MyType> = new ArrayList<com.example.MyType>();

A challenge

Class<?> clazz = Class.forName("com.example.MyType");

Mission impossible

List<String> classes = getClassNamesFromUrl("http://example.com/classes.txt");
for (String className:classes) {
   doSomethingWith(Class.forName(className));
}

I support Jon's advice to look at the byte code (BCEL) but just be aware, that in general it is not possible to read all dependencies from a jar, as they can be dynamic and defined outside the library (see: Mission impossible).


Hard to tell if there's a tool, have a look at those directories on java-source.net:

Further reading

Andreas_D
I want to try this with J2ME application. "Possible" case is enough for me. Any tools?
anupsth
+1 for the nice examples. The "Mission impossible" is neat.
Nils Schmidt