2

For a given Java source code file I want to list all the (fully qualified names of) classes that are (directly) required for compilation. In other words: All classes that are directly used by the code in the source code file, coming from imports, fully qualified names in the source code and other compile time means, but not by reflection or other runtime means.

Is there a way to "ask" the java compiler for this list? Are there other ways to get it?

PS: By "directly" I mean the following: If my source code file requires class A for compilation which uses class B, then class B has to be present to compile the code, but it is not a direct use.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • Theoretically, one could isolate the source code for class `A` from the other classes and try to compile. You will now see all error messages for missing classes that are not included in the jdk. Copy required classes to the source tree and compile until there are no errors related to missing classes anymore. Keep in mind that you need the complete source code and will not receive `java.*`-classes. And it will probably take some time – msrd0 Oct 09 '16 at 17:00

1 Answers1

0

You can try this:

It can be eaily done with just javac. Delete your class files and then compile the main class. javac will recursively compile any classes it needs (providing you don't hide package private classes/interfaces in strangely named files).

Community
  • 1
  • 1
mm759
  • 1,404
  • 1
  • 9
  • 7
  • Could you be more specific how you would apply the technique to find all required classes? The the required classes may not be present in other source code files, but might come from other jars. – J Fabian Meier Oct 09 '16 at 17:08
  • Sorry, I realize that the does not solve your problem because 1. there are already compiled dependencies that you mentioned and 2. you want only direct dependencies. – mm759 Oct 09 '16 at 19:57