6

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

For example - if below two classes are packaged into Myjar.jar

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(){
    }
}

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

Forgot to mention, i don't have access to sourcecode.

Andrii Kalytiiuk
  • 1,501
  • 14
  • 26
anupsth
  • 657
  • 1
  • 6
  • 18

5 Answers5

5

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

Community
  • 1
  • 1
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • I want to try this with J2ME application. "Possible" case is enough for me. Any tools? – anupsth Jul 20 '10 at 07:29
  • I would like to create a plugin system, that allows limited functionality for the plugin. So limited that not the excluded, but the allowed classes and methods can be listed. Besides simple types, collections and classes defined in the JAR nothing else can be used, not even Class, to avoid reflection tricks. What would be the best method to check the JAR containing the plugin, or the source code of the plugin? – Andras Balázs Lajtha Jan 15 '13 at 10:30
  • But even in the Mission Impossible case, code in your JAR file must have been written against some interface that the code knows about. It might be good enough just to know what interfaces corresponding to external implementations your JAR depends on. – mbmast Jun 28 '22 at 19:27
3

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Check untangle

It's a small cli utility that searches usages of class or packages inside jars or directories containing classes.

Disclaimer: I'm the author of untangle

0

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
  • 68,052
  • 28
  • 140
  • 210
  • 1
    I tried JDepend. I saw only package names. I am not able to see class names and method names. – anupsth Jul 20 '10 at 07:21
0

JavaDepend could help you for such needs, you can for any code elements get all elements used, it can be jar, namespace, class or method.

CQL an SQL like to query code base gives you more flexibility to request any info about your code.