In Java how do I list all packages and classes available at run time? #765
-
I'm playing with reflection, writing a search form with suggest-auto-complete. Ultimately I'd like either a way to pull all packages and classes OR have a way to list all packages and classes immediately in the current package level (even at root level), the same way that when I start typing I tried to build a list of all packages and classes like this: public class ClassPathScanner {
public static Set<String> getVisiblePackagesAndClasses() {
Set<String> classNames = new HashSet<>();
try (ScanResult scanResult = new ClassGraph()
.enableAllInfo()
.acceptPackages("java", "javax", "com.marginallyclever") //, "com", "org", "jdk", "sun"
.scan()) {
classNames.addAll(scanResult.getAllClasses().getNames());
}
return classNames;
}
} It omitted all java.* packages. example, java.math and java.lang are not in the results (see end). What am I doing wrong? How do I get it right? My Maven Thank you!
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Before you scan, call |
Beta Was this translation helpful? Give feedback.
-
Glad it works for you! |
Beta Was this translation helpful? Give feedback.
Before you scan, call
.enableSystemModulesAndPackages()
or similar (I may have the name slightly wrong, this is off the top of my head).