Originally posted by Alex Jacobsen:
If I have the name for a source file as a string, and I want to find all the packages used by that source file, how would I go about doing it within the program? Assuming the source file is in the same directory as the program.
all i can think of is to read the source file and scan for any "import" statements. it's kindof brute-force, and it won't pick up any packages used by the packages that the source file imports, but maybe it's a start at least...?
Also, remember that you don't have to import classes to use them. You'll also need to parse the source code for any fully-qualified class names throughout. Finally, while unlikely, a class can use "Class.forName("some.package.you.dont.know.about.MyClass");" It's a thorny problem for sure.
Originally posted by M Beck:
all i can think of is to read the source file and scan for any "import" statements.
If the class is compiled, maybe you could load it using your own ClassLoader subclass that logs each Class that gets loaded. I'm not very familiar with this, but I believe that loads all related classes immediately -- not just when they are instantiated. This would also give you the full dependency list as the load should load all necessary classes recursively.
Again, that's my assumption. I've never tried it.