1

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:

  • Do all Class entries in the constant pool table refer to a class that is used somewhere in the class file?
  • Or are there other mechanisms how a Class entry may get into the constant pool?
  • 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?
towi
  • 21,587
  • 28
  • 106
  • 187
  • The class entries in the constant pool are all classes that are *somehow* referenced by your class. My memory's a little fuzzy on this (and, IIRC the situation changes dependent on security options), but I believe that it's possible that a few of the references are not "required" and the class can execute without them. And not all of the referenced classes need to be found and loaded to load the class and begin execution, but there's probably no good way to tell which are needed. – Hot Licks Mar 21 '14 at 15:06

3 Answers3

2

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.

Antimony
  • 37,781
  • 10
  • 100
  • 107
  • Thanks, that sounds authoritative. And special thanks for pointing "java agents" out to me, a known language fetishist. Now I will not be able to get any sleep until I wrote my own Class Transformer ;-) – towi Mar 21 '14 at 21:30
0

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

aryann
  • 909
  • 5
  • 11
0

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)

javaBeginner
  • 83
  • 2
  • 8