how to know which class / package/ methods used by given jar file?
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");
开发者_StackOverflow中文版 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.
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:
- Open Source ByteCode Libraries in Java
- Open Source Code Analyzers in Java (has some applications to work on jars too, like JDepend. JarAnalyzer sounds promising too, but it is quite old, last update in 2005)
Further reading
- How can I visualize jar (not plugin) dependencies? (especially VonC's answer)
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.
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
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.
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.
精彩评论