towi
towi

Reputation: 22337

How to find out the dependent classes from a raw class file?

When I look at a binary class file can I easily find out what other classes need to be loaded in order to use this class?

As a sort of "directory" I can only see the Constant Pool Table with Class entries. The 1st and 2nd entry has a specific meaning, directly explained in the JVM Spec I found and elsewhere. But are the other entries in that table a list of other classes used by this class file? So to speak the JVM variant of the import-section of the *.java-file?

So it boils down to:

Upvotes: 1

Views: 1406

Answers (3)

javaBeginner
javaBeginner

Reputation: 93

jdeps is a very powerful tool (since java 8), that will show you all the dependencies. It traverses through the classfiles in the given Folder, class- or jar-file and show you the dependencies within the application. Just run "jdeps -verbose " and it will give you the Information u need and probably even more. You can read a little about it here: [http://docs.oracle.com/javase/8/docs/technotes/tools/unix/jdeps.html] and here [https://www.voxxed.com/blog/2014/12/jdeps-compact-profiles-java-modularity/]

(pardon my english but its not my first language)

Upvotes: 0

Antimony
Antimony

Reputation: 39471

Do all Class entries in the constant pool table refer to a class that is used somewhere in the class file?

No, you can always put in constant pool entries that aren't actually used. A class compiled with a standard compiler will only contain entries that are actually used though.

Or are there other mechanisms how a Class entry may get into the constant pool?

No, the constant pool is fixed. Though the file on your disk may not represent the actual class that is loaded, since it's always possible for a custom class loader or Java agent to manipulate things at runtime.

Assuming I wold not like to implement some kind of "lazy class loading" on first use of a class, am I therefore loading the correct list of used classes by using the class entries in the constant pool table?

For static analysis, that's the best you can do. Note that you will miss dependencies used through reflection, but there's not much you can do about that.

Upvotes: 2

aryann
aryann

Reputation: 949

In Java 8, the jdeps command-line tool is provided for analysing the dependencies of a class files.

Upvotes: 0

Related Questions